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
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-get-current-date
Get Current Date Time - javatpoint
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.
๐ŸŒ
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 } }
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-current-date-time
Java - Current Date and Time - GeeksforGeeks
July 11, 2025 - // java program to use Date and ... void main(String[] args){ // Current date LocalDate currentDate = LocalDate.now(); System.out.println("Current date: " + currentDate); // Current time LocalTime currentTime = LocalTime.now(); ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javatime โ€บ javatime_localdate_now.htm
java.time.LocalDate.now() Method Example
The following example shows the usage of java.time.LocalDate.now() method. package com.tutorialspoint; import java.time.LocalDate; public class LocalDateDemo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println(date); } }
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ java-get-current-date
Get Current Date and Time in Java - Tpoint Tech
March 17, 2025 - There are many ways to get current the date and time in Java. There are many classes that can be used to get current date and time in Java. java.time.format.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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-8-date-localdate-localdatetime-instant
Java 8 Date - LocalDate, LocalDateTime, Instant | DigitalOcean
August 3, 2022 - This class provides an overloaded method for now() where we can pass ZoneId for getting dates in a specific time zone. This class provides the same functionality as java.sql.Date.
๐ŸŒ
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
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").
๐ŸŒ
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 Java `java.time.*` APIs , we can use `.now()` to get the current date-time and format it with DateTimeFormatter.
๐ŸŒ
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()); } }...
๐ŸŒ
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 - In Spring, Java developers are accustomed to auto-wiring services that have implementations configured differently for runtime vs. test-time. This approach requires that each time the current time is required in a service, instead of calling ZonedDateTime.now(), you call this new service:
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_date_time.htm
Java - Date and Time
It would be a bit silly if you had to supply the date multiple times to format each part. For that reason, a format string can indicate the index of the argument to be formatted. The index must immediately follow the % and it must be terminated by a $. import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", date); } }
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-date
Java Date and Time - Javatpoint
Class class is part of the java.time package which is added to the Java 8 release as the Java Date and Time API (also known as JSR-310). The date and time are brought together in this class without having timezone information.
๐ŸŒ
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 - It's still useful to know how to ... without time. This means that we can only get the current date, but without the time of the day: LocalDate date = LocalDate.now(); // Gets the current date...