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
To display the current date and time, import the java.time.LocalDateTime class, and use its now() method: import java.time.LocalDateTime; // import the LocalDateTime class public class Main { public static void main(String[] args) { LocalDateTime ...
🌐
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 date: Calendar currentUtilCalendar = Calendar.getInstance(); ... This class is particularly useful for performing arithmetic operations, like adding or subtracting days.
🌐
Mkyong
mkyong.com › home › java › java – how to get current date time
Java - How to get current date time - Mkyong.com
March 22, 2021 - DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println(dateFormat.format(cal.getTime())); // 2021/03/22 16:37:15 · For the java.time.LocalDate, uses LocalDate.now() to get the current date without a time-zone, and format it with the DateTimeFormatter.
🌐
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.
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - Import the necessary package. ... import java.time.LocalDateTime; LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("Current Date and Time: " + currentDateTime); Explain Code
🌐
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 offers many useful ways to get current date or current time using Date, Calendar and newly introduced LocalDate, LocalDateTime and ZonedDateTime classes in Java 8 Date/Time API classes.
Find elsewhere
🌐
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
— use any of the follow “now” methods on these Java classes to get the current data and time: import java.time.*; System.out.println( LocalDate.now() ); // 2024-04-14 System.out.println( LocalTime.now() ); // 20:10:26.039386576 System.out.println( LocalDateTime.now() ); // 2024-04-14...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-current-date-in-java
How to get the current date in Java?
import java.time.LocalDate; public class CreateDate { public static void main(String args[]) { LocalDate date = LocalDate.now(); System.out.println("Current Date: "+date); } } ... The getInstance() (without arguments) method of the this class returns the Calendar object representing the current ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-get-todays-date
Java Program to Get Today's Date - GeeksforGeeks
July 23, 2025 - ... now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone. This method will return LocalDate based on the system clock with the default time-zone to obtain the current date.
🌐
ZetCode
zetcode.com › java › currentdatetime
Java current date time - how to get current date time in Java
It is a modern replacement of the outdated java.util date and time API. java.time.Instant models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application. ... import java.time.Instant; void main() { Instant instant = Instant.now(); System.out.println(instant); } The code example uses java.time.Instant to get the current date and time.
🌐
Coderanch
coderanch.com › t › 369242 › java › grab-current-date
How to grab current date (Java in General forum at Coderanch)
Can some body suggest some thing easier. Kishore. ... If you're just trying to get the current date, new Date() is much simpler than creating a Calendar. A Calendar is only necessary if you want to manipulate the individual fields like month, day of month, day of week, hourse, minutes, seconds, etc.
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-17.php
Java - Date before and after 1 year from the current date
import java.util.*; public class Exercise17 { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); Date cdate = cal.getTime(); // get next year cal.add(Calendar.YEAR, 1); Date nyear = cal.getTime(); //get previous year cal.add(Calendar.YEAR, -2); Date pyear = cal.getTime(); System.out.println("\nCurrent Date : " + cdate); System.out.println("\nDate before 1 year : " + pyear); System.out.println("\nDate after 1 year : " + nyear+"\n"); } } ... Current Date : Tue Jun 20 17:21:52 IST 2017 Date before 1 year : Mon Jun 20 17:21:52 IST 2016 Date after 1 year : Wed Jun 20 17:21:52 IST 2018 · N.B.: The result may varry for your system date and time. ... Write a Java program to calculate the date exactly one year before and one year after the current date.
🌐
Educative
educative.io › answers › how-to-get-the-current-date-and-time-in-java
How to get the current date and time in Java
The format method of the DateFormat class is used to obtain the current date and time (same as the previous method). In this method, the getTime() method of the Calendar is passed onto the format method, and an instance of the Calendar class is made using the class’s getInstance() method.
🌐
Coderanch
coderanch.com › t › 532946 › languages › Current-date-mm-dd-yyyy
Current date (mm/dd/yyyy) (Groovy forum at Coderanch)
Using the code below I'm getting the date with a 2 digit year and no leading zeros (4/1/11). <groovy> def dateTime = new Date() def date = dateTime.getDateString() step.setWebtestProperty('send.date', date) </groovy> Thanks, Ted Hey, You should be able to use the SimpleDateFormat class from java.
🌐
W3Docs
w3docs.com › java
How to get the current date/time in Java
import java.time.LocalDate; public class Main { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println("Current Date: " + currentDate); } } This will print the current date in the format "yyyy-MM-dd".