← Back to Articles

Unix Timestamps in Java and Kotlin

Modern Java and Kotlin apps use the Java Time API to produce and consume Unix epoch values with predictable timezone behavior.

Get the current timestamp

Use System.currentTimeMillis() for millisecond precision and Instant.now() for a time-based object.

// Milliseconds since epoch
long millis = System.currentTimeMillis();

// Instant from the system clock
Instant now = Instant.now();
System.out.println(now);
AdSense — In-Article Banner

Convert timestamp to LocalDateTime

Use Instant together with a timezone to convert epoch millis into local date-time values. For timezone-aware handling, convert the same instant to ZonedDateTime.

// Convert epoch milliseconds to LocalDateTime in UTC
Instant instant = Instant.ofEpochMilli(1716584400000L);
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime);

// Or use ZonedDateTime for a timezone-aware value
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
System.out.println(zonedDateTime);

Convert LocalDateTime to epoch

When you have a local date-time object, use toEpochSecond() to get the Unix timestamp in seconds.

// LocalDateTime in UTC zone
LocalDateTime dt = LocalDateTime.of(2026, 5, 24, 15, 30, 0);
long epochSeconds = dt.toEpochSecond(ZoneOffset.UTC);
System.out.println(epochSeconds);
AdSense — In-Article Banner