Unix Timestamp in Java

Java has two main ways to get Unix timestamps: the legacy System.currentTimeMillis() (milliseconds) and the modern java.time.Instant API (Java 8+, recommended).

Ad

Code Examples

Current timestamp — milliseconds (classic)

long ms = System.currentTimeMillis();

Returns milliseconds since epoch. Available since Java 1.0. Widely used in legacy code and Android.

Current timestamp — seconds (classic)

long sec = System.currentTimeMillis() / 1000L;

Integer-divide by 1000 to get seconds. The L suffix ensures 64-bit arithmetic.

Instant API — seconds (Java 8+, recommended)

import java.time.Instant;
long sec = Instant.now().getEpochSecond();

The modern java.time approach. Instant is immutable and timezone-safe. Prefer this over System.currentTimeMillis() in new code.

Instant API — milliseconds (Java 8+)

import java.time.Instant;
long ms = Instant.now().toEpochMilli();

Millisecond precision with the modern Instant API.

Convert timestamp back to date

import java.time.*;
Instant instant = Instant.ofEpochSecond(timestamp);
ZonedDateTime zdt = instant.atZone(ZoneId.of("UTC"));

Convert a Unix timestamp back to a ZonedDateTime for formatting and display.

Note

Java's System.currentTimeMillis() returns milliseconds, not seconds — the same as JavaScript's Date.now(). The java.time package (introduced in Java 8) is the preferred API for new code; avoid the legacy java.util.Date and Calendar classes.

Ad

Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.

← Open the converter