Unix timestamps in Kotlin
On the JVM and on Android (API 26+ or with desugaring), Kotlin uses java.time. Instant is the type that is a Unix timestamp.
Get the current Unix timestamp
import java.time.Instant
val seconds = Instant.now().epochSecond
val millis = System.currentTimeMillis() // or Instant.now().toEpochMilli()
Convert a Unix timestamp to a date
import java.time.Instant
import java.time.ZoneId
val instant = Instant.ofEpochSecond(1700000000) // 2023-11-14T22:13:20Z
val fromMs = Instant.ofEpochMilli(1700000000123)
val kolkata = instant.atZone(ZoneId.of("Asia/Kolkata")) // 2023-11-15T03:43:20+05:30[Asia/Kolkata]
Convert a date to a Unix timestamp
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
val utc = LocalDateTime.of(2026, 11, 14, 10, 0).toEpochSecond(ZoneOffset.UTC) // 1794650400
val ny = LocalDateTime.of(2026, 11, 14, 10, 0)
.atZone(ZoneId.of("America/New_York")).toEpochSecond()
Timezones
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")
.withZone(ZoneId.of("Europe/London"))
fmt.format(Instant.ofEpochSecond(1700000000)) // 2023-11-14 22:13:20 GMT
Common mistakes
- Android below API 26 needs core library desugaring for java.time. Without it, use
System.currentTimeMillis()andjava.util.Date. LocalDateTimehas no zone. Converting it to epoch always needs aZoneIdorZoneOffset, and the choice changes the answer.- Kotlin Multiplatform code can't use java.time. Use
kotlin.time.Instantfrom the standard library (Kotlin 2.3 and later) or kotlinx-datetime.
Complete example
import java.time.Instant
import java.time.ZoneId
fun main() {
println("Now: ${Instant.now().epochSecond}")
val t = Instant.ofEpochSecond(1700000000)
println("UTC: $t")
println("Tokyo: ${t.atZone(ZoneId.of("Asia/Tokyo"))}")
}
Check any value with the epoch converter, or convert a whole column with the batch converter. All examples use 1700000000 (2023-11-14 22:13:20 UTC) and 1794650400 (2026-11-14 10:00:00 UTC).