Unix timestamps in Java
Everything Java needs for Unix timestamps: the current epoch time in seconds and milliseconds, timestamp-to-date and date-to-timestamp conversion, and ISO 8601 parsing. Everything here uses java.time (Java 8+); if you're still touching java.util.Date, convert it to an Instant at the boundary and never look back. There's a live converter at the bottom for checking any value the snippets produce.
Get the current Unix timestamp
long seconds = Instant.now().getEpochSecond(); // 1700000000
long millis = System.currentTimeMillis(); // 1700000000000Convert a timestamp to a date-time
Instant instant = Instant.ofEpochSecond(1700000000L);
ZonedDateTime tokyo = instant.atZone(ZoneId.of("Asia/Tokyo"));
// 2023-11-15T07:13:20+09:00[Asia/Tokyo]An Instant is zone-free by design; attach a ZoneId only when you need a wall-clock rendering.
Convert a date-time to a timestamp
long seconds = zonedDateTime.toInstant().getEpochSecond();
long millis = zonedDateTime.toInstant().toEpochMilli();Parse an ISO 8601 string
Instant instant = Instant.parse("2023-11-14T22:13:20Z");
LocalDateTime local = LocalDateTime.parse("2023-11-14T22:13:20"); // no zone!Instant.parse requires the Z or an offset. A LocalDateTime is a wall clock with no zone; it cannot become a timestamp until you say which zone it was in.
Check a value
Detected: Unix timestamp (seconds)
Your timezone
UTC · GMT+00:00 · UTC
Tue, Nov 14, 2023, 10:13:20 PM
Common formats
UTC
Tue, Nov 14, 2023, 10:13:20 PM
ISO 8601
2023-11-14T22:13:20.000Z
Unix (seconds)
1700000000
Unix (millis)
1700000000000
Relative
…
World clock
Los Angeles PST
Tue, Nov 14, 2023, 02:13:20 PM
GMT-08:00
Chicago CST
Tue, Nov 14, 2023, 04:13:20 PM
GMT-06:00
New York EST
Tue, Nov 14, 2023, 05:13:20 PM
GMT-05:00
London GMT
Tue, Nov 14, 2023, 10:13:20 PM
GMT+00:00
Paris CET
Tue, Nov 14, 2023, 11:13:20 PM
GMT+01:00
Kolkata IST
Wed, Nov 15, 2023, 03:43:20 AM
GMT+05:30
Shanghai CST
Wed, Nov 15, 2023, 06:13:20 AM
GMT+08:00
Tokyo JST
Wed, Nov 15, 2023, 07:13:20 AM
GMT+09:00
Sydney AEDT
Wed, Nov 15, 2023, 09:13:20 AM
GMT+11:00
Common questions
- What's the difference between Instant and LocalDateTime?
- Instant is a point on the UTC timeline (a timestamp); LocalDateTime is a wall-clock reading with no zone attached. Converting between them always requires an explicit ZoneId or ZoneOffset. Java refuses to guess, by design.
- How do I format an Instant for humans?
- instant.atZone(zone).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)). Formatting needs a zone and ideally a locale; the Instant itself has neither.
- Are java.util.Date and Calendar still okay?
- They work but are mutable, confusing, and superseded. Call date.toInstant() at the edge of any legacy API and do all real work in java.time.