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());
🌐
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.
Discussions

Has the precision of Instant.now changed in Java 17?
It has happened in Java 15: https://bugs.openjdk.org/browse/JDK-8242504 More on reddit.com
🌐 r/java
31
72
September 13, 2024
Should java.util.Date be deprecated?
I recently had to tell a co-worker not to use SimpleDateFormat in completely new code. If it was marked deprecated I guess this would not have been needed. More on reddit.com
🌐 r/java
159
221
May 9, 2022
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
import java.time.LocalDate; // import the LocalDate class public class Main { public static void main(String[] args) { LocalDate myObj = LocalDate.now(); // Create a date object System.out.println(myObj); // Display the current date } }
🌐
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 = ...
🌐
Minecraft
minecraft.net › en-us › store › minecraft-deluxe-collection-pc
Minecraft: Java & Bedrock Edition for PC Deluxe Edition | Minecraft
5 days ago - Indecisive gamers rejoice! Minecraft: Java & Bedrock Edition for PC now includes both Java and Bedrock Edition.
Find elsewhere
🌐
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 - Since they all use the local clock ... time NOW is. The next section shows an example of using this approach. The following is a Cucumber feature file showing our test for Taco Tuesday. The scenarios above allow us to test on whatever dates and times we would like. In this case, we test on 2023-08-08, a Tuesday, and on 2023-08-10, a Thursday, and get different results, as expected. This date-time step is set up in the cucumber StepDefinitions.java file as ...
🌐
Medium
medium.com › @AlexanderObregon › javas-instant-now-method-explained-5403bac7ec1e
Java’s Instant.now() Method Explained | Medium
September 13, 2024 - When Instant.now() is called, it returns the current timestamp in UTC (Coordinated Universal Time). This timestamp is represented as an Instant object, which stores the time as seconds and nanoseconds from the standard Java epoch (1970-01-0...
🌐
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.
🌐
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.
🌐
Coderanch
coderanch.com › t › 748511 › java › display-date-time-Java
How do you display date and time in Java? (Beginning Java forum at Coderanch)
January 4, 2022 - Most Java developers these days uses tools like Maven to manage this sort of thing, so if it's a beginner exercise I would just recommend giving up and trying a new project rather than going down that rabbit hole. ... Try this out. It works for me. First import the Date class and create a new Date object, I just use date.
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - In this article, you will learn how to retrieve the current date and time in Java through various examples. Explore different classes like LocalDateTime, ZonedDateTime, Instant, and Date, and understand when and how to use each in your Java applications. Import the necessary package. Use the LocalDateTime.now() method ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › LocalDate.html
LocalDate (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... Serializable, Comparable<ChronoLocalDate>, ChronoLocalDate, Temporal, TemporalAccessor, TemporalAdjuster · public final class LocalDate extends Object implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable · A date without a time-zone in the ...
🌐
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 - To get the timezone-specific date and time information, pass the zone information in the ZonedDateTime.now() method.
🌐
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
July 28, 2024 - No matter which approach you take, those are the steps required to create a java.sql.Date to represent the current date and time (i.e., "now").
🌐
Quora
quora.com › How-do-you-get-current-system-time-in-Java
How to get current system time in Java - Quora
Answer (1 of 5): t̲h̲i̲s̲ ̲m̲e̲a̲n̲s̲ ̲t̲h̲a̲t̲ ̲- ̲T̲o̲ ̲g̲e̲t ̲t̲h̲e̲ ̲c̲u̲r̲r̲e̲nt̲ ̲s̲ys̲t̲e̲m̲ ̲t̲i̲m̲e̲ ̲i̲n̲ ̲J̲a̲v̲a̲ ̲,̲ ̲th̲e̲ ̲s̲i̲m̲p̲l̲e̲s̲t̲ ̲w̲a̲y̲ ̲i̲s̲ ̲t̲o̲ ̲u̲s̲e̲ ̲`̲S̲y̲s̲t̲e̲m̲ ̲.̲c̲u̲r̲r̲e̲n̲t̲T̲i̲m̲e̲M̲i̲l̲l̲i̲s̲(̲)̲`̲ ̲,̲ ̲w̲h̲i̲c̲h ̲g̲i̲v̲e̲s ̲y̲o̲u̲ ̲t̲h̲e̲ ̲e̲p̲o̲c̲h̲ ...
🌐
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 ... static void main(String[] args) { // Printing Today's date by calling // java.time.LocalDate.now() function System.out.println(java.time.LocalDate.now()); } }...
🌐
Attacomsian
attacomsian.com › blog › java-get-current-date-time
How to get current date and time in Java
October 14, 2022 - Optionally, if you want to display the current date and time in a different format, you can use the SimpleDateFormat class to format the Date object as shown below: // get the current date and time Date now = new Date(); // print date object ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › current-date-time-in-java
How to get current date and time in java
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); Calendar calobj = Calendar.getInstance(); System.out.println(df.format(calobj.getTime())); import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class GettingCurrentDate ...
🌐
Sololearn
sololearn.com › en › Discuss › 2573114 › how-to-display-current-date-and-time-in-java
how to display current date and time in java | Sololearn: Learn to code for FREE!
Here's are some of the ways to get current time or date , forgive me if I missed to include other ways. import java.util.*; import java.time.LocalDate; public class Program { public static void main(String[] args) { //calender instance Date date1 = Calendar.getInstance().getTime(); //util date Date date2 = new Date(); //sql date java.sql.Date sqlDate= new java.sql.Date(date2.getTime()); //from java 8 LocalDate local = LocalDate.now(); System.out.println(date1); System.out.println(date2); System.out.println(sqlDate); System.out.println(local); } } 2nd Nov 2020, 10:48 AM ·