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.
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.
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:
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:
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:
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:
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
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.time vs legacy classes
| Class | Package | Thread-Safe | Timezone Handling | Java Version |
Instant | java.time | Yes | UTC only | 8+ |
ZonedDateTime | java.time | Yes | Explicit timezone | 8+ |
LocalDateTime | java.time | Yes | No timezone | 8+ |
DateTimeFormatter | java.time | Yes | Configurable | 8+ |
Date | java.util | No | System default | 1.0+ |
SimpleDateFormat | java.text | No | Configurable | 1.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:
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:
A quick check: a 13-digit value is likely milliseconds, a 10-digit value likely seconds.
Common format patterns
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.
Related reading
- How to convert float to int with Java
- How to convert hashmap to JSON object in Java
- How to convert int[] into List<Integer> in Java?
- How to convert Java String into byte[]?
- How to convert java.sql.timestamp to LocalDate java8 java.time?
- How to convert java.util.Date to java.sql.Date?
- How to convert jsonString to JSONObject in Java
- How to convert List to Map?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.