Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Kotlin

🎈 🎈 🎈
+1

    import java.time.Instant
    import java.time.ZoneId
    import java.time.format.DateTimeFormatter

    fun main() {
        val unixTimestamp = 1111181961L
        val dateTime = Instant.ofEpochSecond(unixTimestamp)
            .atZone(ZoneId.systemDefault())
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

        println("Converted Date: $dateTime")
    }
        

Output:
Converted Date: 2005-03-18 21:39:21
Example only. There may be multiple ways to perform this operation.

Code Explanation

The Unix timestamp is converted to an Instant using Instant.ofEpochSecond(unixTimestamp), which handles the conversion from seconds to an instant in time.
This instant is then adjusted to the system's default time zone using (ZoneId.systemDefault()), resulting in a ZonedDateTime. Finally, the ZonedDateTime is formatted into a readable date and time string using DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").


Other useful Kotlin date functions

Function Description Example
LocalDate.now() Returns the current date in the format 'YYYY-MM-DD'. LocalDate.now() returns 2024-08-28
LocalTime.now() Returns the current time in the format 'HH:mm:ss'. LocalTime.now() returns 12:34:56
LocalDateTime.now() Returns the current date and time in the format 'YYYY-MM-DDTHH:mm:ss'. LocalDateTime.now() returns 2024-08-28T12:34:56
Instant.now() Returns the current timestamp in UTC. Instant.now() returns 2024-08-28T12:34:56Z
Instant.ofEpochSecond() Converts a Unix timestamp (seconds since the Unix epoch) to an Instant. Instant.ofEpochSecond(1693227296L) returns 2024-08-28T12:34:56Z
LocalDate.parse() Parses a string to create a LocalDate object. LocalDate.parse("2024-08-28") returns 2024-08-28
LocalDateTime.parse() Parses a string to create a LocalDateTime object. LocalDateTime.parse("2024-08-28T12:34:56") returns 2024-08-28T12:34:56
ZonedDateTime.now() Returns the current date and time with the time zone information. ZonedDateTime.now() returns 2024-08-28T12:34:56+02:00[Europe/Paris]
DateTimeFormatter.ofPattern() Formats a date or time object according to a custom pattern. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()) returns 2024-08-28 12:34:56
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