What's the best way to get the current date/time in Java?

There is no "best" way.

It depends on what form of date / time you want:

  • If you want the date / time as a single numeric value, then System.currentTimeMillis() gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Java long). This value is a delta from a UTC time-point, and is independent of the local time-zone1.

  • If you want the date / time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:

    • new Date() gives you a Date object initialized with the current date / time. The problem is that the Date API methods are mostly flawed ... and deprecated.

    • Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone. Other overloads allow you to use a specific Locale and/or TimeZone. Calendar works ... but the APIs are still cumbersome.

    • new org.joda.time.DateTime() gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here. (But note that some people report that Joda time has performance issues.; e.g. https://stackoverflow.com/questions/6280829.)

    • in Java 8, calling java.time.LocalDateTime.now() and java.time.ZonedDateTime.now() will give you representations2 for the current date / time.

Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.

With Java 8 and later, the standard java.time package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate3.


Note: the Calendar, org.joda.time and java.time solutions can use either the platform's default timezone or an explicit timezone provided via constructor arguments. Generally, using an explicit timezone rather than the default zone will make your application's behavior more predictable / less susceptible to problems if (for example) you redeploy to a data center in a different timezone.

But no matter what you do, you (and maybe your application) should be aware that the timezone of the user, your service and the data center can all be different. The concept of the "current date/time" is complicated.


1 - System.currentTimeMillis() gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.

Answer from Stephen C on Stack Overflow
Top answer
1 of 16
792

What's the best way to get the current date/time in Java?

There is no "best" way.

It depends on what form of date / time you want:

  • If you want the date / time as a single numeric value, then System.currentTimeMillis() gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Java long). This value is a delta from a UTC time-point, and is independent of the local time-zone1.

  • If you want the date / time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:

    • new Date() gives you a Date object initialized with the current date / time. The problem is that the Date API methods are mostly flawed ... and deprecated.

    • Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone. Other overloads allow you to use a specific Locale and/or TimeZone. Calendar works ... but the APIs are still cumbersome.

    • new org.joda.time.DateTime() gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here. (But note that some people report that Joda time has performance issues.; e.g. https://stackoverflow.com/questions/6280829.)

    • in Java 8, calling java.time.LocalDateTime.now() and java.time.ZonedDateTime.now() will give you representations2 for the current date / time.

Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.

With Java 8 and later, the standard java.time package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate3.


Note: the Calendar, org.joda.time and java.time solutions can use either the platform's default timezone or an explicit timezone provided via constructor arguments. Generally, using an explicit timezone rather than the default zone will make your application's behavior more predictable / less susceptible to problems if (for example) you redeploy to a data center in a different timezone.

But no matter what you do, you (and maybe your application) should be aware that the timezone of the user, your service and the data center can all be different. The concept of the "current date/time" is complicated.


1 - System.currentTimeMillis() gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.

2 of 16
451

(Attention: only for use with Java versions <8. For Java 8+ check other replies.)

If you just need to output a time stamp in format YYYY.MM.DD-HH.MM.SS (very frequent case) then here's the way to do it:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
To display the current date, import the java.time.LocalDate class, and use its now() method:
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-current-date-time
Java - Current Date and Time - GeeksforGeeks
July 11, 2025 - // java program to use Date and ... static void main(String[] args){ // Current date LocalDate currentDate = LocalDate.now(); System.out.println("Current date: " + currentDate); // Current time LocalTime currentTime = ...
🌐
Baeldung
baeldung.com › home › java › java dates › get the current date and time in java
Get the Current Date and Time in Java | Baeldung
January 8, 2024 - In this article, we explored various approaches to working with dates and times before and after Java 8+. We learned how to retrieve the current date, time, and timestamp using LocalDate, LocalTime, and Instant. We also delved into the pre-Java 8 classes, including java.util.Date, java.util.Calendar, and java.sql.Date.
🌐
Alvin Alexander
alvinalexander.com › java › java-current-date-example-now
Java: How to get the current date (and time) in Java 8, 11, 14, 17, etc. | alvinalexander.com
// create a java calendar instance ... = calendar.getTime(); // now, create a java.sql.Date from the java.util.Date java.sql.Date date = new java.sql.Date(currentDate.getTime());...
🌐
Mkyong
mkyong.com › home › java › java – how to get current date time
Java - How to get current date time - Mkyong.com
March 22, 2021 - For Java `java.time.*` APIs , we can use `.now()` to get the current date-time and format it with DateTimeFormatter.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Date.html
Date (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... The class Date represents a specific instant in time, with millisecond precision.
🌐
How to do in Java
howtodoinjava.com › home › java date time › get current date and time in java
Get Current Date and Time in Java - HowToDoInJava
April 4, 2023 - The main classes were : ... Let us have a quick look at the methods used for getting the current date and time information in legacy Java classes. Date date = new Date(); System.out.println(date); //Tue Feb 15 13:00:31 IST 2022 Calendar cal ...
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - Retrieve the current date and time in a specific time zone using ZonedDateTime.now(). ... import java.time.ZonedDateTime; import java.time.ZoneId; ZonedDateTime zonedDatetime = ZonedDateTime.now(ZoneId.of("America/New_York")); System.out.println("Current Date and Time in New York: " + zonedDatetime); Explain Code
🌐
Javatpoint
javatpoint.com › java-get-current-date
Get Current Date and Time in Java
How to Get Current Date and Time in Java examples using java.time.LocalDate, java.time.Calendar, java.time.LocalTime, java.util.Date, java.sql.Date and Calendar classes. We can get current date and time int java by different ways.
🌐
ZetCode
zetcode.com › java › currentdatetime
Java current date time - how to get current date time in Java
The code example uses java.time.Instant to get the current date and time. ... The Instant.now method obtains the current instant from the system clock.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-8-date-localdate-localdatetime-instant
Java 8 Date - LocalDate, LocalDateTime, Instant | DigitalOcean
August 3, 2022 - Separation of Concerns: The new ... clearly defined and perform the same action in all the classes. For example, to get the current instance we have now() method....
🌐
Coderanch
coderanch.com › t › 401987 › java › create-date-object-current-date
How to create a date object which contain current date (Beginning Java forum at Coderanch)
... Hey, you guys sound really mean to this poor person...lol, anyways i would do what they said, if you wanted to see the current date in a readable manner, you would do something like: Date now= new Date()//to get a stamp of the current date System.out.println("The current date and time ...
🌐
Stack Abuse
stackabuse.com › how-to-get-current-date-and-time-in-java
How to Get Current Date and Time in Java
April 16, 2020 - This means that we can only get the current date, but without the time of the day: LocalDate date = LocalDate.now(); // Gets the current date · This time around, instead of initializing a new object, we're calling the static method now() which returns the current date according to the system ...
🌐
Quora
quora.com › How-do-I-get-todays-date-in-Java
How to get today's date in Java - Quora
Answer (1 of 5): Minimally, [code ]new java.util.Date();[/code] However, this is hard to test. So I like to use an interface [code]interface Clock { Date now(); } [/code]and have a class for production [code]class ProductionClock implements Clock { public Date now() { return new Date()...
Top answer
1 of 10
443

Just construct a new Date object without any arguments; this will assign the current date and time to the new object.

import java.util.Date;

Date d = new Date();

In the words of the Javadocs for the zero-argument constructor:

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Make sure you're using java.util.Date and not java.sql.Date -- the latter doesn't have a zero-arg constructor, and has somewhat different semantics that are the topic of an entirely different conversation. :)

2 of 10
58

tl;dr

Instant.now()

java.time

The java.util.Date class has been outmoded by the new java.time package (Tutorial) in Java 8 and later. The old java.util.Date/.Calendar classes are notoriously troublesome, confusing, and flawed. Avoid them.

ZonedDateTime

Get the current moment in java.time.

ZonedDateTime now = ZonedDateTime.now();

A ZonedDateTime encapsulates:

  • Date.
  • Time-of-day, with a fraction of a second to nanosecond resolution.
  • Time zone.

If no time zone is specified, your JVM’s current default time zone is assigned silently. Better to specify your desired/expected time zone than rely implicitly on default.

ZoneId z = ZoneId.of( "Pacific/Auckland" );
ZonedDateTime zdt = ZonedDateTime.now( z );

UTC

Generally better to get in the habit of doing your back-end work (business logic, database, storage, data exchange) all in UTC time zone. The code above relies implicitly on the JVM’s current default time zone.

The Instant class represents a moment in the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.now();

The Instant class is a basic building-block class in java.time and may be used often in your code.

When you need more flexibility in formatting, transform into an OffsetDateTime. Specify a ZoneOffset object. For UTC use the handy constant for UTC.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );

Time Zone

You easily adjust to another time zone for presentation to the user. Use a proper time zone name, never the 3-4 letter codes such as EST or IST.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime nowMontreal = instant.atZone( z );

Generate a String representation of that date-time value, localized.

String output = DateTimeFormatter
    .ofLocalizedDate( FormatStyle.FULL )
    .withLocale( Locale.CANADA_FRENCH )
    .format ( nowMontreal );

Instant

Or, to stay in UTC, use Instant. An Instant object represents a moment on the timeline, to nanosecond resolution, always in UTC. This provides the building block for a zoned date-time, along with a time zone assignment. You can think of it conceptually this way:

ZonedDateTime = Instant + ZoneId

You can extract an Instant from a ZonedDateTime.

Instant instantNow = zdt.toInstant();

You can start with an Instant. No need to specify a time zone here, as Instant is always in UTC.

Instant now = Instant.now();
🌐
LinkedIn
linkedin.com › pulse › how-get-current-system-date-time-javaselenium-while-testing-uçar
How to Get the Current System Date and Time in Java/Selenium While Testing
August 9, 2023 - Here is an example of how to use the LocalDate class to get the current system date in Java: LocalDate currentDate = LocalDate.now(); String currentDateString = currentDate.toString();
🌐
Medium
medium.com › @kaustubh.saha › java-new-date-and-time-apis-3736358e9e57
Java new Date and Time APIs. For years, Java developers grappled… | by Kaustubh Saha | Medium
October 17, 2025 - It is immutable (just like other classes in the new API) Instant.MIN (one billion+ years ago) and Instant.MAX (midnight on Dec 31st, 1 billion AD) defines the lower and upper limits of time that an Instant can represent. To create an Instant object that represents the current time, we can use Instant.now()