Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Scala

How to convert Datetime to Unix timestamp in Scala

🎈 🎈 🎈
+1

    import java.time.{LocalDateTime, ZoneOffset}

    val dateTime: LocalDateTime = LocalDateTime.of(2023, 10, 28, 12, 44, 12)
    val unixTimestamp: Long = dateTime.toEpochSecond(ZoneOffset.UTC)
    
    println(s"Unix Timestamp: $unixTimestamp")            
        

Output:
Unix Timestamp: 1698497052
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Scala example the LocalDateTime.of() method is used to convert a Unix timestamp into a LocalDateTime object representing this specific date and time. To convert this LocalDateTime into a Unix timestamp, we use the toEpochSecond() method, passing ZoneOffset.UTC to ensure the conversion is based on Coordinated Universal Time (UTC).
The toEpochSecond() method calculates the number of seconds between the Unix epoch (January 1, 1970, 00:00:00 UTC) and the specified date and time, resulting in the Unix timestamp 1698497052.



Other useful Scala date functions

Function Description Example
java.time.LocalDate.now() Returns the current date in the format 'YYYY-MM-DD'. java.time.LocalDate.now() returns 2024-08-28
java.time.LocalTime.now() Returns the current time in the format 'HH:mm:ss'. java.time.LocalTime.now() returns 12:34:56
java.time.LocalDateTime.now() Returns the current date and time in the format 'YYYY-MM-DDTHH:mm:ss'. java.time.LocalDateTime.now() returns 2024-08-28T12:34:56
java.time.Instant.now() Returns the current timestamp in UTC. java.time.Instant.now() returns 2024-08-28T12:34:56Z
java.time.LocalDate.parse() Parses a string to create a LocalDate object. java.time.LocalDate.parse("2024-08-28") returns 2024-08-28
java.time.LocalDateTime.parse() Parses a string to create a LocalDateTime object. java.time.LocalDateTime.parse("2024-08-28T12:34:56") returns 2024-08-28T12:34:56
java.time.LocalDateTime.of() Creates a LocalDateTime from specified date and time values. java.time.LocalDateTime.of(2024, 8, 28, 12, 34, 56) returns 2024-08-28T12:34:56
java.time.Instant.ofEpochMilli() Converts milliseconds since Unix epoch to an Instant. java.time.Instant.ofEpochMilli(1693227296789L) returns 2024-08-28T12:34:56.789Z
java.time.Instant.ofEpochSecond() Converts seconds since Unix epoch to an Instant. java.time.Instant.ofEpochSecond(1693227296L) returns 2024-08-28T12:34:56Z
java.time.LocalDateTime.toEpochSecond() Converts a LocalDateTime to Unix timestamp (seconds since epoch) with specified time zone. java.time.LocalDateTime.now().toEpochSecond(java.time.ZoneOffset.UTC) returns 1693227296
java.time.LocalDateTime.format() Formats the LocalDateTime using a DateTimeFormatter. java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) returns 2024-08-28 12:34:56
java.time.ZonedDateTime.now() Returns the current date and time with the time zone information. java.time.ZonedDateTime.now() returns 2024-08-28T12:34:56+02:00[Europe/Paris]
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder