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.ofEpochSecondandofEpochMilliare easy to swap — the compiler cannot help you because both take along.LocalDateTimehas no zone and no instant. Converting it to an epoch requires supplying aZoneId; there is no correct default.- Always pass an explicit
ZoneIdrather than relying onZoneId.systemDefault(), which differs between your laptop and the container.
Rules that apply in every language
- Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
- Name the unit in the identifier.
expires_at_msrather thanexpires_atcosts nothing and prevents the single most common timestamp bug. - Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
- 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
- Python float seconds
- JavaScript integer milliseconds
- PHP integer seconds
- Go integer seconds and nanoseconds
- TypeScript integer milliseconds
- Ruby float seconds
- C# ticks (100 ns)
- Rust seconds + nanoseconds
- SQL varies by engine
- Bash integer seconds
- C++ chrono duration
- Swift float seconds
- Kotlin integer milliseconds
- C time_t seconds
- Perl integer seconds
- Dart integer milliseconds
- PowerShell .NET DateTimeOffset
- Scala integer milliseconds
- R float seconds
- Excel days since 1899-12-30
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.