Convert Unix Timestamp in Java

Java 8+ provides the java.time package for modern date/time handling. Use Instant for Unix timestamps—it supports both seconds and milliseconds. See also: Instant vs LocalDateTime.

Unix Timestamp to Instant/Date

Java
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

// From seconds
long timestampSeconds = 1704067200L;
Instant instant = Instant.ofEpochSecond(timestampSeconds);
System.out.println(instant); // 2024-01-01T00:00:00Z

// From milliseconds
long timestampMs = 1704067200000L;
Instant instantFromMs = Instant.ofEpochMilli(timestampMs);
System.out.println(instantFromMs); // 2024-01-01T00:00:00Z

// With nanoseconds
Instant precise = Instant.ofEpochSecond(1704067200L, 500_000_000L);
System.out.println(precise); // 2024-01-01T00:00:00.500Z

Instant/Date to Unix Timestamp

Java
import java.time.Instant;

// Current timestamp in seconds
long nowSeconds = Instant.now().getEpochSecond();
System.out.println(nowSeconds); // e.g., 1704067200

// Current timestamp in milliseconds
long nowMs = System.currentTimeMillis();
// or: Instant.now().toEpochMilli()
System.out.println(nowMs); // e.g., 1704067200000

// From specific Instant
Instant instant = Instant.parse("2024-01-01T00:00:00Z");
long timestamp = instant.getEpochSecond();
System.out.println(timestamp); // 1704067200

Timezone Handling

Java
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

long timestamp = 1704067200L;
Instant instant = Instant.ofEpochSecond(timestamp);

// Convert to specific timezone
ZonedDateTime utc = instant.atZone(ZoneId.of("UTC"));
System.out.println(utc); // 2024-01-01T00:00Z[UTC]

ZonedDateTime newYork = instant.atZone(ZoneId.of("America/New_York"));
System.out.println(newYork); // 2023-12-31T19:00-05:00[America/New_York]

ZonedDateTime tokyo = instant.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(tokyo); // 2024-01-01T09:00+09:00[Asia/Tokyo]

// Convert between timezones
ZonedDateTime laTime = newYork.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
System.out.println(laTime); // 2023-12-31T16:00-08:00[America/Los_Angeles]

Formatting

Java
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

long timestamp = 1704067200L;
ZonedDateTime zdt = Instant.ofEpochSecond(timestamp)
    .atZone(ZoneId.of("UTC"));

// Various formats
DateTimeFormatter iso = DateTimeFormatter.ISO_ZONED_DATE_TIME;
System.out.println(zdt.format(iso)); // 2024-01-01T00:00:00Z[UTC]

DateTimeFormatter custom = DateTimeFormatter
    .ofPattern("MMMM d, yyyy 'at' HH:mm z", Locale.US);
System.out.println(zdt.format(custom)); // January 1, 2024 at 00:00 UTC

DateTimeFormatter dateOnly = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(zdt.format(dateOnly)); // 2024-01-01

Common Pitfalls

Using Legacy Date/Calendar

Avoid java.util.Date and Calendar. They have design flaws and timezone issues.

// ❌ Legacy approach - avoid
Date date = new Date(1704067200000L);

// ✅ Modern approach
Instant instant = Instant.ofEpochSecond(1704067200L);

Seconds vs Milliseconds Confusion

Use the right method: ofEpochSecond() for seconds, ofEpochMilli() for milliseconds.

// ❌ Wrong - treating seconds as milliseconds
Instant.ofEpochMilli(1704067200L); // Wrong date in 1970!

// ✅ Correct
Instant.ofEpochSecond(1704067200L); // 2024-01-01

LocalDateTime for Timestamps

LocalDateTime has no timezone. Don't use it for Unix timestamp conversions—use Instantor ZonedDateTime instead.

Edge Cases

Java
import java.time.Instant;

// Negative timestamps (before 1970)
Instant before1970 = Instant.ofEpochSecond(-86400L);
System.out.println(before1970); // 1969-12-31T00:00:00Z

// Maximum supported instant
System.out.println(Instant.MAX); // +1000000000-12-31T23:59:59.999999999Z
System.out.println(Instant.MIN); // -1000000000-01-01T00:00:00Z

// Nanosecond precision
Instant nano = Instant.ofEpochSecond(1704067200L, 123456789L);
System.out.println(nano); // 2024-01-01T00:00:00.123456789Z
System.out.println(nano.getNano()); // 123456789

Frequently Asked Questions

What class should I use for Unix timestamps in Java?

Use java.time.Instant for Unix timestamps. It represents an instant on the timeline in UTC. Avoid the legacy Date and Calendar classes.

Does Java use seconds or milliseconds for Unix timestamps?

Java supports both. Instant.ofEpochSecond() takes seconds, while Instant.ofEpochMilli() takes milliseconds. System.currentTimeMillis() returns milliseconds.

How do I handle timezones in Java?

Use ZonedDateTime for timezone-aware operations. Convert an Instant to a ZonedDateTime using atZone(ZoneId). Always store and transmit as Instant (UTC).

What is the difference between Instant and LocalDateTime?

Instant represents a point in time (UTC). LocalDateTime represents a date and time without timezone information. Use Instant for timestamps, LocalDateTime for user-facing calendar dates.