For Developers

Unix Timestamp in Kotlin

How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Kotlin — plus why java.time, not java.util.Date, is worth the extra line.

val ts = System.currentTimeMillis() // ms
val tsSeconds = Instant.now().epochSecond
val seconds = 1750000000L
val instant = Instant.ofEpochSecond(seconds)
val utc = instant.atZone(ZoneOffset.UTC)
val zdt = ZonedDateTime.of(2025, 6, 15, 12, 0, 0, 0, ZoneOffset.UTC)
val ts = zdt.toEpochSecond()

The Kotlin-specific pitfall

Kotlin runs on the JVM, so it inherits the same trap Java developers hit: System.currentTimeMillis() and the legacy java.util.Date API are still the first thing autocomplete suggests, and they carry all of the old design flaws (mutable, no real timezone awareness, months indexed from zero). Kotlin code should reach for java.time.Instant and ZonedDateTime from the start — they're immutable, explicit about UTC vs. local, and the interop with Kotlin's null-safety is far cleaner. On Android specifically, java.time needs API 26+ or the desugaring library on older targets.

Have a timestamp right now?

Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.

Related

Related tools & reading