Unix Timestamp in Java

Modern Java uses the java.time package from Java 8 onwards. Instant is the epoch type; the legacy java.util.Date should be avoided in new code.

Need to convert one value rather than write code? Use the interactive converter — it handles every unit and time zone without leaving your browser.

Current Unix timestamp

import java.time.Instant;

System.currentTimeMillis();               // 1710050400000  (milliseconds)
Instant.now().getEpochSecond();           // 1710050400     (seconds)
Instant.now().toEpochMilli();

Timestamp to date

import java.time.*;
import java.time.format.DateTimeFormatter;

Instant instant = Instant.ofEpochSecond(1710050400L);

ZonedDateTime zoned = instant.atZone(ZoneId.of("Asia/Kolkata"));
System.out.println(zoned);                // 2024-03-10T11:30+05:30[Asia/Kolkata]

System.out.println(DateTimeFormatter
    .ofPattern("yyyy-MM-dd HH:mm:ss z")
    .format(zoned));

Date to timestamp

import java.time.*;

ZonedDateTime zdt = ZonedDateTime.of(
    LocalDateTime.of(2024, 3, 10, 11, 30, 0),
    ZoneId.of("Asia/Kolkata"));

zdt.toEpochSecond();                      // 1710050400
zdt.toInstant().toEpochMilli();

Instant.parse("2024-03-10T06:00:00Z").getEpochSecond();

Interop with legacy Date

import java.util.Date;
import java.time.Instant;

Date legacy = Date.from(Instant.ofEpochSecond(1710050400L));
Instant back = legacy.toInstant();

// java.sql.Timestamp -> Instant
// new java.sql.Timestamp(millis).toInstant();

Pitfalls specific to Java

  • Instant.ofEpochSecond and ofEpochMilli are easy to swap — the compiler cannot help you because both take a long.
  • LocalDateTime has no zone and no instant. Converting it to an epoch requires supplying a ZoneId; there is no correct default.
  • Always pass an explicit ZoneId rather than relying on ZoneId.systemDefault(), which differs between your laptop and the container.

Rules that apply in every language

  1. Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
  2. Name the unit in the identifier. expires_at_ms rather than expires_at costs nothing and prevents the single most common timestamp bug.
  3. Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
  4. Use 64-bit time. Anything still storing seconds in a signed 32-bit field breaks in January 2038 — see the Year 2038 problem.

The same task in other languages

Frequently asked questions

How do I get the current Unix timestamp in Java?

Use the snippet in the "Current Unix timestamp" section above. Java works in integer milliseconds natively, so converting to another unit is a multiplication or an integer division away.

How do I convert a Unix timestamp to a date in Java?

The "Timestamp to date" snippet above shows the idiomatic approach, including how to render the result in a specific time zone rather than whatever zone the machine happens to be set to.

Does Java handle time zones and daylight saving correctly?

Yes, provided you pass an explicit zone rather than relying on the system default. The gotchas listed on this page cover the specific ways Java makes that easy to get wrong.