Java
currentTimeMillis
date conversion
programming tutorial
Java date handling

How to convert System.currentTimeMillis() to a date in Java

Codemia Knowledge Hub ·

Pass the value to Instant.ofEpochMilli(), then convert the Instant to a ZonedDateTime or LocalDateTime for display and formatting. This uses the java.time API from Java 8. The legacy Date class still works but should be avoided in new code.

java
1long millis = System.currentTimeMillis();
2Instant instant = Instant.ofEpochMilli(millis);
3ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
4System.out.println(zdt);
5// Output: 2026-06-18T10:30:00.123-04:00[America/New_York]

What currentTimeMillis() returns

System.currentTimeMillis() returns a long: the number of milliseconds since the Unix epoch (1970-01-01T00:00:00Z). The value is UTC-based and independent of timezone, and is commonly used for timestamps, performance measurement and cache expiration.

java
long now = System.currentTimeMillis();
System.out.println(now);
// Output: 1718700000123

Because the number carries no timezone, turning it into a readable date means choosing a timezone explicitly or using the system default.

Converting with java.time (Java 8+)

Instant

Instant is a point on the UTC timeline and the natural first step from epoch milliseconds:

java
1import java.time.Instant;
2
3long millis = System.currentTimeMillis();
4Instant instant = Instant.ofEpochMilli(millis);
5System.out.println(instant);
6// Output: 2026-06-18T14:30:00.123Z

Instant always prints in UTC (the trailing Z means Zulu/UTC), which removes timezone ambiguity in logs and storage.

ZonedDateTime

To show the time in a specific timezone:

java
1import java.time.Instant;
2import java.time.ZoneId;
3import java.time.ZonedDateTime;
4
5long millis = System.currentTimeMillis();
6Instant instant = Instant.ofEpochMilli(millis);
7
8ZonedDateTime eastern = instant.atZone(ZoneId.of("America/New_York"));
9ZonedDateTime tokyo = instant.atZone(ZoneId.of("Asia/Tokyo"));
10
11System.out.println(eastern); // 2026-06-18T10:30:00.123-04:00[America/New_York]
12System.out.println(tokyo);   // 2026-06-18T23:30:00.123+09:00[Asia/Tokyo]

LocalDateTime

LocalDateTime drops the timezone entirely. Use it for a date and time without timezone context, such as a display where the timezone is handled elsewhere:

java
1import java.time.Instant;
2import java.time.LocalDateTime;
3import java.time.ZoneId;
4
5long millis = System.currentTimeMillis();
6LocalDateTime ldt = LocalDateTime.ofInstant(
7    Instant.ofEpochMilli(millis),
8    ZoneId.systemDefault()
9);
10System.out.println(ldt);
11// Output: 2026-06-18T10:30:00.123

Do not use LocalDateTime to store timestamps. If two servers in different timezones both store LocalDateTime, their values cannot be compared correctly. Use Instant or ZonedDateTime for storage and interchange.

DateTimeFormatter

For custom formats:

java
1import java.time.Instant;
2import java.time.ZoneId;
3import java.time.ZonedDateTime;
4import java.time.format.DateTimeFormatter;
5
6long millis = System.currentTimeMillis();
7ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault());
8
9DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
10System.out.println(zdt.format(formatter));
11// Output: 2026-06-18 10:30:00 EDT

DateTimeFormatter is immutable and thread-safe, so you can keep one in a static final field and share it across threads.

The legacy Date and SimpleDateFormat way

These classes still work and appear often in older code.

Date

java
1import java.util.Date;
2
3long millis = System.currentTimeMillis();
4Date date = new Date(millis);
5System.out.println(date);
6// Output: Thu Jun 18 10:30:00 EDT 2026

Pass the millisecond value to the constructor. new Date() with no argument captures the current time instead, a bug that tends to slip in during refactoring when the millis variable stops being passed through.

SimpleDateFormat

java
1import java.text.SimpleDateFormat;
2import java.util.Date;
3
4long millis = System.currentTimeMillis();
5Date date = new Date(millis);
6
7SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
8System.out.println(sdf.format(date));
9// Output: 2026-06-18 10:30:00

java.time vs legacy classes

ClassPackageThread-SafeTimezone HandlingJava Version
Instantjava.timeYesUTC only8+
ZonedDateTimejava.timeYesExplicit timezone8+
LocalDateTimejava.timeYesNo timezone8+
DateTimeFormatterjava.timeYesConfigurable8+
Datejava.utilNoSystem default1.0+
SimpleDateFormatjava.textNoConfigurable1.1+

Thread safety is the critical difference. SimpleDateFormat keeps internal mutable state, so sharing one instance across threads silently corrupts output or throws exceptions. This is the most common production bug in date formatting. Use DateTimeFormatter, or create a new SimpleDateFormat per thread.

Converting between Date and java.time

When a library still uses java.util.Date, convert at the boundary:

java
1import java.time.Instant;
2import java.time.ZoneId;
3import java.time.ZonedDateTime;
4import java.util.Date;
5
6// Date -> Instant -> ZonedDateTime
7Date legacyDate = new Date(System.currentTimeMillis());
8Instant instant = legacyDate.toInstant();
9ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
10
11// ZonedDateTime -> Instant -> Date
12Date backToLegacy = Date.from(zdt.toInstant());

Use java.time internally and convert to Date only at the edges where legacy APIs require it. Mixing the two types throughout the code makes it hard to reason about.

Milliseconds or seconds?

Timestamps from APIs, databases or message queues may be in milliseconds or seconds. Passing seconds to Instant.ofEpochMilli() gives a date in January 1970:

java
1// Correct: value is in milliseconds
2Instant fromMillis = Instant.ofEpochMilli(1718700000123L);
3
4// Correct: value is in seconds
5Instant fromSeconds = Instant.ofEpochSecond(1718700000L);
6
7// Wrong: passing seconds to ofEpochMilli
8Instant wrong = Instant.ofEpochMilli(1718700000L);
9// This is Jan 20, 1970, not June 2024

A quick check: a 13-digit value is likely milliseconds, a 10-digit value likely seconds.

Common format patterns

java
1import java.time.Instant;
2import java.time.ZoneId;
3import java.time.ZonedDateTime;
4import java.time.format.DateTimeFormatter;
5
6long millis = System.currentTimeMillis();
7ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.of("UTC"));
8
9// ISO 8601 (API responses, logs)
10zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
11// 2026-06-18T14:30:00.123+00:00
12
13// Human-readable
14zdt.format(DateTimeFormatter.ofPattern("MMM dd, yyyy hh:mm a z"));
15// Jun 18, 2026 02:30 PM UTC
16
17// Date only
18zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
19// 2026-06-18
20
21// Compact timestamp for filenames
22zdt.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
23// 20260618_143000

For ISO 8601 output, prefer the built-in DateTimeFormatter.ISO_INSTANT or ISO_OFFSET_DATE_TIME constants over custom patterns. They handle edge cases like leap seconds and timezone offset formatting correctly.

When formatting dates for users in different regions, pass an explicit ZoneId instead of relying on the server's system default, or the local times will be wrong.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms