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
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-current-date-in-java
How to get the current date in Java?
September 6, 2023 - The now() method of the Localdate class returns the Date object representing the current time. ... import java.time.LocalDate; public class CreateDate { public static void main(String args[]) { LocalDate date = LocalDate.now(); System.out.println("Current Date: "+date); } }
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
The package includes many date and time classes. For example: If you don't know what a package is, read our Java Packages Tutorial. To display the current date, import the java.time.LocalDate class, and use its now() method:
🌐
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 tutorial, we’ll explore how to work with dates and times in Java 8+ and prior environments. We’ll start by covering the modern Java 8+ java.time package, then look at legacy approaches for working with dates before Java 8. First, let’s use java.time.LocalDate to get the current system date:
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - This code snippet retrieves the current date and time using the LocalDateTime class. The output includes both the date and time without time zone information, making it ideal for applications not sensitive to time zone discrepancies.
🌐
DevQA
devqa.io › java-get-current-date-time
Java Get Current Date Time
February 7, 2020 - ... import java.time.LocalDate; ... LocalDate.now(); System.out.println(now.toString()); } } ... We can use the DateTimeFormatter class to format the display of the date....
Find elsewhere
🌐
Phrase
phrase.com › home › resources › blog › how to get the current utc date and time in java?
Solved: How to Get the Current UTC Date and Time in Java?
September 23, 2022 - Current Date in milliseconds is :1583954404789 Wed Mar 11 19:20:04 GMT 2020 Wed Mar 11 19:20:04 GMT 2020 Wed Mar 11 19:20:04 UTC 2020 · In general, this isn’t ideal as it displays the current time based on the time zone of the specified region, which may be different than GMT; therefore, you should ideally avoid it. A better and more modern option bundled within the core Java library (and not using any third-party offerings) is to use the java.time package.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-current-date-time
Java - Current Date and Time - GeeksforGeeks
July 11, 2025 - Current date: 2024-01-04 Current time: 11:59:03.285876 Current date and time: 2024-01-04T11:59:03.286975 · This method we will discuss the use of clock method to fetch date and time provided by java.time package.
🌐
Codecademy
codecademy.com › docs › java › date
Java | Date | Codecademy
June 21, 2023 - Date(): Creates an object representing the current date and time. Date(long millis): Creates a Date object with the specified number of milliseconds since January 1, 1970, 00:00:00 GMT (the Unix epoch).
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-get-todays-date
Java Program to Get Today's Date - GeeksforGeeks
July 23, 2025 - // Java Program to Get Today's ... date by calling // java.sql.Date() function long millis = System.currentTimeMillis(); java.sql.Date date = new java.sql.Date(millis); System.out.println(date); } }...
🌐
LabEx
labex.io › tutorials › java-java-localdate-current-date-117826
Mastering Java LocalDate: Discover the Current Date | LabEx
In this lab, we learned how to use the Java LocalDate now(ZoneId) method to obtain the current date from the system clock in the specified time-zone.
🌐
Keyhole Software
keyholesoftware.com › home › testing the current date/time in spring and java
Testing Current Date and Time in Java | Keyhole Software
January 28, 2025 - Other times, we may want to use the current time in a business rule to determine if a date/time being entered is in the future or the past, or maybe we want to check to see if something has expired. Many versions of Java ago, there was java.util.Date; we could simply capture the current time using new Date().
🌐
Automation
learn-automation.com › home › how to get current system date and time in java/ selenium
How to get current system date and time in Java/ Selenium
June 18, 2020 - package demo; import ... object of SimpleDateFormat class and decide the format DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy "); //get current date time with Date() Date date = new Date(); // Now format ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_date_time.htm
Java - Date and Time
You can use a simple Date object with toString() method to print the current date and time as follows − · import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new ...
🌐
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-simpledateformat-java-date-format
Master Java Date Formatting: SimpleDateFormat & DateFormat Guide | DigitalOcean
December 20, 2024 - These classes are part of the java.text package and provide a robust framework for converting dates to strings and vice versa, based on specific locales. SimpleDateFormat allows you to define patterns for date and time formatting, making it highly customizable. For example, you can specify patterns like “dd-MM-yyyy” for dates or “HH:mm:ss” for times. This flexibility is crucial for applications that need to display dates and times in various formats depending on user preferences or regional settings.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › time › package-summary.html
java.time (Java SE 17 & JDK 17)
January 20, 2026 - This stores a date-time like '2010-12-03T11:30+01:00'. This is sometimes found in XML messages and other forms of persistence, but contains less information than a full time-zone. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. The Javadoc "@param" definition is used to summarise the null-behavior.
🌐
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 - In this blog post, I discussed several methods for getting the current system date and time in Java/Selenium while testing. I show that there are four main methods: using the System class, using the Calendar class, using the LocalDate, and using the SimpleDateFormat class.