How to get the current date/time in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In modern Java, the best way to get the current date or time is usually with the java.time API. Which class you choose depends on what you actually mean by "current date/time": a local calendar date, a local date-time, a time with zone information, or a machine-readable instant.
Pick the Right Class First
The main options are:
- '
LocalDatefor just the date' - '
LocalTimefor just the time' - '
LocalDateTimefor date and time without a time zone' - '
ZonedDateTimefor date and time with a zone' - '
Instantfor an absolute point in time'
Choosing the right type is more important than memorizing the method name, because the method is usually just now().
Current Local Date and Time
If you want the current date and time in the system default zone, but you do not need to store the zone explicitly:
This is the most common everyday Java usage.
Current Date and Time with a Zone
If the time zone matters, use ZonedDateTime.
This is better than LocalDateTime when the result must be interpreted consistently across systems.
Machine-Friendly Timestamps with Instant
If you want a precise point on the UTC timeline, use Instant.
Instant is ideal for:
- event timestamps
- database audit fields
- log correlation
- distributed systems
It is usually the best storage type when you do not want ambiguity about time zones.
Formatting the Current Date and Time
Raw toString() output is often fine for debugging, but user-facing output should usually be formatted.
Formatting should happen at the presentation boundary. Keep your internal representation as a proper date-time type.
Legacy APIs
Older Java code often uses:
- '
java.util.Date' - '
java.util.Calendar'
Example:
These APIs still exist, but for new code java.time is the preferred design because it is clearer, more consistent, and much easier to work with correctly.
Choosing by Use Case
A practical rule of thumb is:
- display today's date:
LocalDate.now() - timestamp a local business event:
LocalDateTime.now() - represent a real-world zoned time:
ZonedDateTime.now(...) - persist or compare a global timestamp:
Instant.now()
This avoids a lot of unnecessary confusion later.
Common Pitfalls
Using LocalDateTime when you really need zone-aware or UTC-safe timestamps creates ambiguity across systems.
Formatting too early and storing strings instead of proper date-time types makes later computation harder.
Mixing legacy Date and modern java.time classes without a reason leads to messy code and unnecessary conversions.
Assuming the system default time zone is always the correct business time zone can create subtle bugs in deployed systems.
Summary
- In modern Java, prefer the
java.timeAPI for current date and time values. - Use
LocalDate,LocalTime, orLocalDateTimefor local calendar values. - Use
ZonedDateTimewhen time-zone context matters. - Use
Instantfor absolute timestamps and storage-friendly event times. - Pick the type by use case first, then call
now()on that type.
Related reading
- How to get the current time in YYYY-MM-DD HHMISec.Millisecond format in Java?
- How to get the current working directory in Java?
- How to get the filename without the extension in Java?
- How to get the first non-null value in Java?
- How to get the insert ID in JDBC?
- How to get the last value of an ArrayList
- How to get the name of a class without the package?
- How to get the number of threads in a Java process

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.