Unix Timestamp in Kotlin

Kotlin on the JVM uses java.time.Instant (recommended, Java 8+) or System.currentTimeMillis(). In Kotlin Multiplatform, kotlinx-datetime provides a cross-platform API.

Ad

Code Examples

Current timestamp (seconds) — Instant API

import java.time.Instant
val seconds = Instant.now().epochSecond

The modern JVM approach. Instant is immutable and timezone-safe. Prefer this over System.currentTimeMillis() in new Kotlin code.

Current timestamp (milliseconds)

import java.time.Instant
val ms = Instant.now().toEpochMilli()

Returns milliseconds since epoch as Long. Equivalent to System.currentTimeMillis() but via the modern java.time API.

Classic — System.currentTimeMillis()

val ms = System.currentTimeMillis()
val seconds = ms / 1000L

Works on all JVM targets including older Android versions. Returns milliseconds — divide by 1000 for seconds.

Kotlin Multiplatform — kotlinx-datetime

// build.gradle.kts: implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
import kotlinx.datetime.Clock
val seconds = Clock.System.now().epochSeconds

kotlinx-datetime works on JVM, Android, iOS (via Kotlin/Native), and JS. epochSeconds returns Long.

Convert timestamp to Instant and format

import java.time.*
val instant = Instant.ofEpochSecond(timestamp)
val zdt = instant.atZone(ZoneId.of("UTC"))
val formatted = zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

Reconstruct an Instant from a Unix timestamp, attach a timezone, and format as ISO 8601.

Note

Kotlin on Android historically used System.currentTimeMillis() (milliseconds). The java.time package requires API 26+ on Android, or use desugaring (coreLibraryDesugaring). For Kotlin Multiplatform targeting iOS, kotlinx-datetime is the only cross-platform option.

Ad

Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.

← Open the converter