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
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:
๐ŸŒ
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.
๐ŸŒ
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 - To get the current SQL date: Date currentSqlDate = new Date(System.currentTimeMillis()); We can create a specific date using the valueOf() method, which requires the format yyyy-[mm-[d]d: ... This class is commonly used in database interactions ...
๐ŸŒ
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 ...
๐ŸŒ
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 - 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()); } } Other than that, you can also ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 - java.time.ZonedDateTime โ€“ Represents the date and time information in a given timezone. The following code shows how to get the current date-time information using the now() method in each class.
๐ŸŒ
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.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ how-to-get-current-date-and-time-in-java
How To Get Current Date And Time In Java | Java Date Format | Edureka
June 11, 2021 - The Clock.SystemUTC.instant() method outputs the current date and time. System.out.println(java.time.Clock.systemUTC().instant()); ... The current date injava can be displayed by using the instance ofjava.sql.Date class. long millis=System....
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 05 โ€บ current-date-time-in-java
How to get current date and time in java
import java.util.Date; import ... { //getting current date and time using Date class DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); Date dateobj = new Date(); System.out.println(df.format(dateobj)); /*getting current date time using calendar class * An Alternative ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ get current date and time in java
Get Current Date and Time in Java - Scaler Topics
May 4, 2023 - Then we are printing the time. The format is HH:mm: ss. zzz. The java. time.LocalDateTime class is an immutable class, which is used to fetch the date and time with a default format of yyyy-MM-dd-HH-mm-ss.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-get-the-current-date-and-time-in-java
How to get the current date and time in Java
A lot of programming tasks make use of the current date and time in Java. There are two ways to obtain this: Using the Date class. Using the Calendar class. The SimpleDateFormat class will also be used to get the date and time into the desired ...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ current-date-time
Java Program to Get Current Date/Time
For default format, it is simply converted from a LocalDateTime object to a string internally using a toString() method. import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CurrentDateTime { public static void main(String[] args) { LocalDateTime current = ...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ 7-ways-get-current-date-time-java-terala-chittibabu
7 ways to get current date & time in java
July 4, 2022 - Java 8 introduced LocalDate class in java.time package, which simplifies getting current date. It has a static now() method which returns the current date.
๐ŸŒ
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 - Getting the current date and time is really easy using a calendar:
๐ŸŒ
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:
๐ŸŒ
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 new Java 8 java.time.* APIs , we can use .now() to get the current date-time and format it with DateTimeFormatter. For legacy date-time APIs, we can use new Date() and Calendar.getInstance() to get the current date-time and format it with ...
๐ŸŒ
Java67
java67.com โ€บ 2016 โ€บ 02 โ€บ how-to-get-current-date-and-time-in-java.html
How to get Current Date and Time in Java with Example | Java67
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels. ... You can get today's date or current date and time in Java by creating a new Date() object. A java.util.Date object represents the current date and time instance in Java.