Unix Timestamp in Kotlin
Kotlin on the JVM uses java.time directly: Instant.now().epochSecond for the current second and Instant.ofEpochSecond(ts) to convert back. System.currentTimeMillis() returns milliseconds.
Get & convert epoch time in Kotlin
import java.time.Instant val t = Instant.ofEpochSecond(1700000000L) // 2023-11-14T22:13:20.000Z val now = Instant.now().epochSecond
Gotcha: On Kotlin Multiplatform the JVM time APIs aren't available everywhere — use kotlinx-datetime's Clock.System.now() for portability.
Frequently asked questions
- How do I get the current Unix timestamp in Kotlin?
- Instant.now().epochSecond on the JVM, or Clock.System.now().epochSeconds with kotlinx-datetime.
- How do I convert a timestamp in Kotlin?
- Instant.ofEpochSecond(ts) builds an Instant you can format in any zone.