Unix timestamps in Java

Use java.time (Java 8 and later). Instant is a point on the UTC timeline and maps directly onto a Unix timestamp; the older Date and Calendar classes work in milliseconds.

Current timestamp

long seconds = Instant.now().getEpochSecond();    // e.g. 1763164800
long millis  = System.currentTimeMillis();        // e.g. 1763164800123
long millis2 = Instant.now().toEpochMilli();      // the same value from java.time

Timestamp to date

Every example uses 1700000000, which is 14 November 2023 at 22:13:20 UTC, so you can compare the output across languages or check it in the timestamp converter.

Instant i = Instant.ofEpochSecond(1700000000L);
System.out.println(i);                                         // 2023-11-14T22:13:20Z
System.out.println(Instant.ofEpochMilli(1700000000123L));      // 2023-11-14T22:13:20.123Z

ZonedDateTime ny = i.atZone(ZoneId.of("America/New_York"));
System.out.println(ny);                                        // 2023-11-14T17:13:20-05:00[America/New_York]
System.out.println(ny.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));   // 2023-11-14 17:13:20

An Instant prints in UTC and has no zone. Call atZone to get calendar fields, and pass ZoneOffset.UTC when you want them in UTC.

Date to timestamp

Instant.parse("2023-11-14T22:13:20Z").getEpochSecond();                          // 1700000000
OffsetDateTime.parse("2023-11-14T17:13:20-05:00").toEpochSecond();               // 1700000000
LocalDateTime.of(2023, 11, 14, 22, 13, 20).toEpochSecond(ZoneOffset.UTC);        // 1700000000
ZonedDateTime.of(2023, 11, 14, 17, 13, 20, 0, ZoneId.of("America/New_York")).toEpochSecond();   // 1700000000

A LocalDateTime has no zone, which is why toEpochSecond makes you supply an offset: the same wall-clock time is a different instant in each zone.

Pitfalls

  • Integer overflow. 1700000000 * 1000 is computed in 32-bit int arithmetic and wraps to a wrong value before it is widened. Write 1700000000L * 1000.
  • new Date(long) takes milliseconds, as do Timestamp and Calendar.setTimeInMillis. Passing seconds gives January 1970.
  • ZoneId.systemDefault() differs between your laptop and the server. Name the zone, or use UTC, anywhere the result is stored or compared.
  • Legacy classes. java.util.Date is also just an instant, but its toString() prints in the default zone, which misleads people into thinking it has one. Convert with date.toInstant() and Date.from(instant).

Questions

How do I get the current Unix timestamp in Java?

Instant.now().getEpochSecond() for seconds, or System.currentTimeMillis() for milliseconds.

Does System.currentTimeMillis() depend on the time zone?

No. It is the number of milliseconds since 1970-01-01T00:00:00Z, the same in every zone.

Should I use System.nanoTime() for timestamps?

No. nanoTime() counts from an arbitrary origin and is only for measuring elapsed time. For a nanosecond-precision wall-clock value use Instant.now(), which exposes getEpochSecond() and getNano().