Java Timestamp Converter
Java has evolved significantly in its approach to time handling. The modern java.time API (introduced in Java 8) is far superior to the older java.util.Date and java.util.Calendar classes. For Unix timestamp handling in Java, the Instant class is the primary tool — it represents a specific moment on the timeline with nanosecond precision and has direct methods for converting to and from Unix timestamps.
If you're working with legacy code that uses java.util.Date, be aware that Date.getTime() returns milliseconds (not seconds), and the constructor also takes milliseconds. This means a seconds-precision Unix timestamp must be multiplied by 1000 before being passed to new Date(ts) — the same pattern as JavaScript.
Java timestamp code examples (modern java.time API)
Get current timestamp in seconds:
Instant.now().getEpochSecond() // long
Get current timestamp in milliseconds:
System.currentTimeMillis() // long, milliseconds
or: Instant.now().toEpochMilli()
Unix seconds to Instant:
Instant instant = Instant.ofEpochSecond(1700000000L);
Unix milliseconds to Instant:
Instant instant = Instant.ofEpochMilli(1700000000000L);
Instant to LocalDateTime (UTC):
LocalDateTime.ofInstant(instant, ZoneOffset.UTC)
LocalDateTime to Unix seconds:
LocalDateTime.of(2026, 4, 17, 10, 35, 0).toEpochSecond(ZoneOffset.UTC)
Format as ISO 8601:
DateTimeFormatter.ISO_INSTANT.format(instant) // "2023-11-14T22:13:20Z"