Blog
Wild & Free Tools

Convert Unix Timestamp in Java — Instant and ZonedDateTime Methods

Last updated: April 2026 7 min read

Table of Contents

  1. Instant Class
  2. ZonedDateTime
  3. Date to Timestamp
  4. Java 7 Legacy
  5. Frequently Asked Questions

Java has the cleanest time API of any major language since Java 8 — but only if you ignore the legacy java.util.Date class entirely. The new java.time package handles Unix timestamps with three classes: Instant, LocalDateTime, and ZonedDateTime. Each one solves a different problem.

This guide shows when to use which, with copy-paste code for every common conversion. If you are still on Java 7 or earlier, the legacy methods are at the end.

Use Instant for Unix Timestamps

Instant is Java's direct representation of a Unix timestamp. It stores nanoseconds since the epoch and has no timezone — exactly like a real Unix timestamp.

import java.time.Instant;

// From Unix timestamp in seconds
long unixSeconds = 1711000000L;
Instant instant = Instant.ofEpochSecond(unixSeconds);
System.out.println(instant); // 2024-03-21T07:46:40Z

// From milliseconds
long unixMs = 1711000000123L;
Instant instant2 = Instant.ofEpochMilli(unixMs);

// Current time
Instant now = Instant.now();

// Back to Unix timestamp
long ts = instant.getEpochSecond();
long tsMs = instant.toEpochMilli();

Always store and pass Instant through your application code. Convert to a zoned representation only at the display layer. This pattern eliminates 90% of timezone bugs.

Display an Instant in a Specific Timezone

To show a Unix timestamp in a specific timezone, convert the Instant to a ZonedDateTime with a ZoneId.

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

Instant instant = Instant.ofEpochSecond(1711000000L);

// Display in New York time
ZonedDateTime ny = instant.atZone(ZoneId.of("America/New_York"));
System.out.println(ny); // 2024-03-21T03:46:40-04:00[America/New_York]

// Display in Tokyo time
ZonedDateTime tokyo = instant.atZone(ZoneId.of("Asia/Tokyo"));

// Format as a custom string
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatted = ny.format(fmt); // "2024-03-21 03:46:40"

The same Instant gives you different ZonedDateTime values for different zones — but the underlying instant in time is identical. This is the correct mental model: instants are absolute, zones are display.

LocalDateTime — when to NOT use it

LocalDateTime has no timezone information at all. It represents "March 21, 2024 at 3:46 PM" without saying where. Use it only for things like birthdays or business hours that are conceptually local. Never use it to represent an event that happened at a specific moment — that is what Instant or ZonedDateTime are for.

Sell Custom Apparel — We Handle Printing & Free Shipping

Convert a Java Date to a Unix Timestamp

Going from a date string or Date object to a Unix timestamp depends on what you started with.

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

// From an ISO 8601 string
Instant fromIso = Instant.parse("2024-03-21T07:46:40Z");
long ts = fromIso.getEpochSecond();

// From a custom format string
LocalDateTime ldt = LocalDateTime.parse("2024-03-21 07:46:40",
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
long ts2 = ldt.toInstant(ZoneOffset.UTC).getEpochSecond();

// From a Java util Date (legacy code)
java.util.Date legacyDate = new java.util.Date();
long ts3 = legacyDate.toInstant().getEpochSecond();

The legacy java.util.Date class is misleadingly named — it actually represents an instant in time, not a date. Its getTime() method returns milliseconds since epoch. New code should never create java.util.Date objects directly.

Pre-Java 8 Code (Legacy)

If you are stuck on Java 7 or earlier, you have to use java.util.Date and SimpleDateFormat. They are clunky and not thread-safe, but they work.

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

// Unix timestamp to Date
long unixSeconds = 1711000000L;
Date date = new Date(unixSeconds * 1000); // Date wants milliseconds

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = sdf.format(date);

// Date to Unix timestamp
long ts = date.getTime() / 1000;

Three things to remember on legacy Java: java.util.Date uses milliseconds (multiply seconds by 1000), SimpleDateFormat is not thread-safe (never make it a static field), and the default TimeZone is whatever the JVM was started with — always set it explicitly.

If you need to verify a single value without firing up a JVM, the free Unix timestamp converter handles it in your browser.

Try It Free — No Signup Required

Runs 100% in your browser. No data is collected, stored, or sent anywhere.

Open Free Unix Timestamp Converter

Frequently Asked Questions

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

Use Instant.ofEpochSecond(timestamp) for seconds or Instant.ofEpochMilli(timestamp) for milliseconds. The Instant class is the modern Java representation of a Unix timestamp and has no timezone — convert to ZonedDateTime when you need to display it in a specific zone.

What is the difference between Instant and LocalDateTime in Java?

Instant represents a specific moment in time (like a Unix timestamp) and is always in UTC. LocalDateTime represents a date and time without any timezone, like "noon on Wednesday" without saying where. Use Instant for events, LocalDateTime for things like birthdays or local business hours.

How do I get the current Unix timestamp in Java?

Instant.now().getEpochSecond() returns the current Unix timestamp in seconds. Instant.now().toEpochMilli() returns milliseconds. Both are based on the system clock at the moment the call is made.

Why does new Date(timestamp) show 1970?

java.util.Date expects milliseconds since the epoch, not seconds. If you pass a 10-digit Unix timestamp directly without multiplying by 1000, it interprets the value as a few minutes after January 1, 1970. Always: new Date(unixSeconds * 1000).

Is SimpleDateFormat thread-safe?

No. SimpleDateFormat is famously not thread-safe and will silently corrupt data if shared between threads. Use DateTimeFormatter from java.time.format instead, which is thread-safe and immutable.

How do I display a Unix timestamp in a specific timezone in Java?

Convert the Instant to a ZonedDateTime with a ZoneId: instant.atZone(ZoneId.of("America/New_York")). The instant itself never changes — only the displayed values for hour, day, etc. shift to match the target zone.

Launch Your Own Clothing Brand — No Inventory, No Risk