Java
Unix Timestamp
Date Conversion
Programming
Coding

Java Date from unix timestamp

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a Unix timestamp in Java is easy once you know whether the timestamp is expressed in seconds or milliseconds. The bigger design choice is whether you truly need the old java.util.Date type or whether you should convert into the newer java.time API and only bridge to Date when required by legacy code.

Understand the Units First

A Unix timestamp usually means the number of seconds since 1970-01-01T00:00:00Z, but many APIs and databases use milliseconds instead. Mixing those up is the most common bug in these conversions.

If the value is in seconds, multiply by 1000 before creating a legacy Date.

java
1import java.util.Date;
2
3public class Demo {
4    public static void main(String[] args) {
5        long unixSeconds = 1700000000L;
6        Date date = new Date(unixSeconds * 1000L);
7        System.out.println(date);
8    }
9}

If the value is already in milliseconds, do not multiply again.

java
1import java.util.Date;
2
3public class Demo {
4    public static void main(String[] args) {
5        long unixMillis = 1700000000000L;
6        Date date = new Date(unixMillis);
7        System.out.println(date);
8    }
9}

Prefer Instant in Modern Java

For new code, java.time.Instant is usually the better representation. It models a moment on the timeline directly and avoids much of the awkwardness of the old Date API.

java
1import java.time.Instant;
2
3public class Demo {
4    public static void main(String[] args) {
5        long unixSeconds = 1700000000L;
6        Instant instant = Instant.ofEpochSecond(unixSeconds);
7        System.out.println(instant);
8    }
9}

If your input is milliseconds instead of seconds, use Instant.ofEpochMilli.

java
1import java.time.Instant;
2
3public class Demo {
4    public static void main(String[] args) {
5        long unixMillis = 1700000000000L;
6        Instant instant = Instant.ofEpochMilli(unixMillis);
7        System.out.println(instant);
8    }
9}

This is generally the cleanest answer unless an older API specifically demands Date.

Convert the Instant to a Time Zone

A Unix timestamp is always a point in time, but humans usually want to view that moment in a specific zone.

java
1import java.time.Instant;
2import java.time.ZoneId;
3import java.time.ZonedDateTime;
4
5public class Demo {
6    public static void main(String[] args) {
7        long unixSeconds = 1700000000L;
8        Instant instant = Instant.ofEpochSecond(unixSeconds);
9        ZonedDateTime torontoTime = instant.atZone(ZoneId.of("America/Toronto"));
10        System.out.println(torontoTime);
11    }
12}

This is much clearer than relying on the default JVM timezone implicitly.

Bridge Between Instant and Date When Needed

If a legacy library needs java.util.Date, you can still keep your internal logic in java.time and convert only at the boundary.

java
1import java.time.Instant;
2import java.util.Date;
3
4public class Demo {
5    public static void main(String[] args) {
6        Instant instant = Instant.ofEpochSecond(1700000000L);
7        Date legacyDate = Date.from(instant);
8        System.out.println(legacyDate);
9    }
10}

The reverse conversion is also simple:

java
1import java.time.Instant;
2import java.util.Date;
3
4public class Demo {
5    public static void main(String[] args) {
6        Date date = new Date();
7        Instant instant = date.toInstant();
8        System.out.println(instant);
9    }
10}

This lets you keep the stronger API internally while staying compatible with older code.

Common Pitfalls

The most common pitfall is mixing seconds and milliseconds. If the resulting year looks absurdly far in the future or near 1970, the unit is usually wrong.

Another issue is assuming Date contains timezone information. It does not. It represents an instant, and the timezone only appears when you format or display it.

Developers also often keep using Date for new code just because older examples do. In modern Java, Instant, ZonedDateTime, and related types are usually the better default.

Finally, avoid relying on the system default timezone unless that is genuinely part of the requirement. Explicit zones produce more predictable code.

Summary

  • Check first whether the Unix timestamp is in seconds or milliseconds.
  • Use new Date(seconds * 1000L) only when you specifically need java.util.Date.
  • Prefer Instant.ofEpochSecond or Instant.ofEpochMilli in new Java code.
  • Convert to ZonedDateTime when you need a human-readable local time.
  • Treat timezone formatting and epoch conversion as separate concerns.

Course illustration
Course illustration

All Rights Reserved.