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 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 ...
🌐
BeginnersBook
beginnersbook.com › 2017 › 09 › java-8-get-current-date-and-time
Java 8 – Get Current Date and Time
Java 8 introduces a new date and time API java.time.* which has several classes, but the ones that we can use to get the date and time are: java.time.LocalDate, java.time.LocalTime & java.time.LocalDateTime. class DateExample { public static void main(String[] args) { /* Obtains the current date from the system clock in the * default time-zone.
🌐
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
ZonedDateTime.now(ZoneId.of("zone")) gets the current date and time for a specific time zone. Sentry BlogException Handling in Java (with Real Examples) (opens in a new tab) Syntax.fmListen to the Syntax Podcast (opens in a new tab)
🌐
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 - For JDK 8 or later, the recommended way is to use LocalDate and LocalTime classes. For JDK 7 or earlier, we can use of Date and Calendar classes only. In Java 8 or later, the date and time information is represented by the following classes.
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-today-s-date-in-java8
How to get today's date in Java8?
Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.
🌐
Attacomsian
attacomsian.com › blog › java-get-current-date-time
How to get current date and time in Java
October 14, 2022 - Current Instant: 2019-12-20T22:52:00.870Z EPOCH Seconds: 1576882320 EPOCH Milliseconds: 1576882320870 · Read Introduction to Java 8 Date and Time API tutorial for more new date and time API examples.
🌐
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
As you can see, this sample Java program shows how to connect to a MySQL database, construct a Java Date object, create a SQL INSERT statement, use that INSERT statement in a Java PreparedStatement, and then execute this SQL INSERT statement, which inserts our Java current date into the database.
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-get-the-current-date-and-next-date-using-localdate-in-java-414036
How to get the current date and next date using LocalDate in Java | LabEx
In this tutorial, you have learned how to work with dates in Java using the LocalDate class from the Java Time API. Here is a summary of what you accomplished: Created a Java program to retrieve the current date using LocalDate.now()
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-8-date-localdate-localdatetime-instant
Java 8 Date - LocalDate, LocalDateTime, Instant | DigitalOcean
August 3, 2022 - This class provides the same ... LocalDateExample { public static void main(String[] args) { //Current Date LocalDate today = LocalDate.now(); System.out.println("Current Date="+today); //Creating LocalDate by providing input arguments LocalDate firstDay_2014 = ...
🌐
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.
🌐
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 - LocalDateTime currentDateTime = LocalDateTime.now() DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); System.out.println(formatter.format(currentDateTime)); // 04/07/2022 12:59:24; LocalTime in java 8 represents ...
🌐
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 ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
How to get current date time with Java 8 - Java Code Geeks
January 11, 2021 - Output: Current Date Time using Instant:2021-01-09T22:25:06.385917300Z · If you want to know date time, in your time zone, at a particular moment( represented by Instant) you can use ZonedDateTime. You can either adjust the instant created above to get the ZonedDateTime like below:
🌐
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 - ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone ... DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); System.out.println(dateTime.format(formatter)); ... Another class introduced to us in the Java 8 ...
🌐
W3Docs
w3docs.com › java
How to get the current date/time in Java
If you just want the current date, you can use the LocalDate class instead: import java.time.LocalDate; public class Main { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println("Current Date: " ...
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - Create an instance with new Date(), which captures the current date and time. ... import java.util.Date; Date date = new Date(); System.out.println("Current Date and Time: " + date); Explain Code · This snippet uses the older Date class from ...