Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Scala

🎈 🎈 🎈
+1

    import java.time.{Instant, LocalDateTime, ZoneId}

    val unixTimestamp: Long = 1714412428L
    val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(unixTimestamp), ZoneId.systemDefault())
    
    println(s"Converted Date: $dateTime")
        

Output:
Converted Date: 2024-04-29 17:40:28
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Scala example, the Unix timestamp 1714412428L is converted into a human-readable date using the Instant.ofEpochSecond() method to create an Instant object that corresponds to the specific moment in time represented by the Unix timestamp.
Then, we convert this Instant to a LocalDateTime object using LocalDateTime.ofInstant(), adjusting for the system's default time zone with ZoneId.systemDefault(). The result is a LocalDateTime object that represents the date and time 2024-04-29 17:40:28 in the local time zone, which is then printed in a human-readable format.



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