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
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
To display the current time (hour, minute, second, and nanoseconds), import the java.time.LocalTime class, and use its now() method:
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());
Discussions

How to use 'clj-time' to get current time by LocalDateTime? - Beginners - ClojureVerse
How to use 'clj-time' to get current time by LocalDateTime · Basically the meaning of ‘local’ in the .local namespace is that those functions handle DateTime objects (with time zones) in the local time zone, while LocalDateTime is timezone-less DateTime · Not sure if there’s something ... More on clojureverse.org
🌐 clojureverse.org
0
February 11, 2025
Selenium:Java:Compare current time with a specific time - Archive - The Club: Software Testing & Quality Engineering Community Forum | Ministry of Testing
Hello There, I need help in comparing two time(s) and validate a selenium test in Java My requirement is - Validate if a counter is present on my application between 1:00am to 2:00pm I am able to get the below Calend… More on club.ministryoftesting.com
🌐 club.ministryoftesting.com
4
January 8, 2022
how to get the current time as 'yyyy-mm-dd' in clojure?
(.format (new java.text.SimpleDateFormat "yyyy-MM-dd") (java.util.Date.)) More on reddit.com
🌐 r/Clojure
5
7
August 19, 2015
How can I get the time length of a video file and change the time position of a video with a slider for my Media Player?
Media information is asynchronous, meaning your code will continue to run without it completing. This solution may help lead you in the right direction, at least for your first issue. https://stackoverflow.com/questions/27208201/javafx-unknown-duration-on-media-object More on reddit.com
🌐 r/javahelp
3
18
November 3, 2019
🌐
Sentry
sentry.io › sentry answers › java › how do i get the current date and time in java?
How do I get the current date and time in Java? | Sentry
January 15, 2025 - You can the current date and time using classes from the java.time package introduced in Java 8. We will demonstrate how to use the following recommended java.time classes: ... Note: You need to be running Java version 8 or later to use java.time classes. The LocalDateTime class is commonly used to get the current date and time without time zone information:
🌐
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 - Therefore, if you want to get swiftly ... code: package com.thdespou; import java.util.Date; public class Main { public static void main(String[] args) { // Date Date now = new Date(); System.out.println("Current Date in milliseconds is :" + now.getTime()); } }...
🌐
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.
Find elsewhere
🌐
Quora
quora.com › How-do-you-get-current-system-time-in-Java
How to get current system time in Java - Quora
Read the documentation. Long answer: In Java, the System class gives you a static PrintStream member variable called out. This gives you an output stream that represents the console.
🌐
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); } }
🌐
ClojureVerse
clojureverse.org › community center › beginners
How to use 'clj-time' to get current time by LocalDateTime? - Beginners - ClojureVerse
February 11, 2025 - (require '[clj-time.local :as l]) (type (l/to-local-date-time (l/local-now))) => org.joda.time.DateTime but how to get the object by “org.joda.time.LocalDateTime”? thank you.
🌐
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 - This code will first create a Date object for the current time. Then, it will create a SimpleDateFormat object with the format yyyy-MM-dd HH:mm:ss. Finally, it will use the SimpleDateFormat object to format the Date object into a string. The LocalDate class is a new class in Java 8 that represents a date without a time zone. This class is useful for getting the current date in a specific format, without having to worry about the time zone.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › Clock.html
Clock (Java Platform SE 8 )
5 days ago - Most applications should avoid ... on the time-line rather than a raw millisecond value. This method is provided to allow the use of the clock in high performance use cases where the creation of an object would be unacceptable. The default implementation currently calls instant(). ... the current millisecond instant from this clock, measured from the Java epoch of 1970-01-01T00:00Z (UTC), not null ... Gets the current ...
🌐
Attacomsian
attacomsian.com › blog › java-get-current-date-time
How to get current date and time in Java
October 14, 2022 - Current Time: 03:30:08.116 Current Formatted Time: 03:30 AM · The LocalDateTime class, the most popular date and time class in Java, holds both local date and time without any timezone information. Here is an example that demonstrates how you can use LocalDateTime to get the current date and time in Java 8 and higher:
🌐
Medium
medium.com › @AlexanderObregon › javas-localtime-now-explained-cb7c516ef5d3
Java’s LocalTime.now() Explained | Medium
September 19, 2024 - Java introduced the java.time package in Java 8 as part of the new Date and Time API, which aimed to simplify and improve the handling of date and time. One useful method within this package is LocalTime.now(), which returns the current time without any date information.
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - Use Instant.now() to get the GMT/UTC time. ... import java.time.Instant; Instant timestamp = Instant.now(); System.out.println("Current GMT/UTC Time: " + timestamp); Explain Code
🌐
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 ...
🌐
Ministry of Testing
club.ministryoftesting.com › archive
Selenium:Java:Compare current time with a specific time - Archive - The Club: Software Testing & Quality Engineering Community Forum | Ministry of Testing
January 8, 2022 - Hello There, I need help in comparing two time(s) and validate a selenium test in Java My requirement is - Validate if a counter is present on my application between 1:00am to 2:00pm I am able to get the below Calend…
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › package-summary.html
java.time (Java Platform SE 8 )
5 days ago - Each date time instance is composed of fields that are conveniently made available by the APIs. For lower level access to the fields refer to the java.time.temporal package. Each class includes support for printing and parsing all manner of dates and times.
🌐
Coderanch
coderanch.com › t › 687251 › java › current-time
how to get current time value? (Beginning Java forum at Coderanch)
November 15, 2017 - I think the best approach for me would create a Time Server, and I did it as well. I wrote a code on the server which returns the server's current Date-Time. As of now, I just ran the java program on server's terminal(and kept the program running) and tested that from my local.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Date.html
Date (Java Platform SE 8 )
5 days ago - This Date object is modified so that it represents a point in time within the specified minute of the hour, with the year, month, date, hour, and second the same as before, as interpreted in the local time zone. ... Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.SECOND). Returns the number of seconds past the minute represented by this date. The value returned is between 0 and 61. The values 60 and 61 can only occur on those Java Virtual Machines that take leap seconds into account.