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
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:
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());
๐ŸŒ
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 - 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 date:
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Date.html
Date (Java Platform SE 8 )
1 day ago - Javaโ„ข Platform Standard Ed. 8 ... The class Date represents a specific instant in time, with millisecond precision.
๐ŸŒ
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.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-get-current-date
Get Current Date and Time in Java
January 28, 2025 - 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.
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
March 22, 2021 - 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()
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-get-the-current-date-in-java
How to get the current date in Java?
July 10, 2021 - The now() method of the Localdate class returns the Date object representing the current time. ... import java.time.LocalDate; public class CreateDate { public static void main(String args[]) { LocalDate date = LocalDate.now(); System.out.println("Current Date: "+date); } }
๐ŸŒ
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 ... date by calling // java.sql.Date() function long millis = System.currentTimeMillis(); java.sql.Date date = new java.sql.Date(millis); System.out.println(date); } }...
๐ŸŒ
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.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ date
Java | Date | Codecademy
June 21, 2023 - Date(): Creates an object representing the current date and time. Date(long millis): Creates a Date object with the specified number of milliseconds since January 1, 1970, 00:00:00 GMT (the Unix epoch).
๐ŸŒ
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
July 4, 2022 - If for some reason you need to use a solution that we used in the days before Java 8, read below for those old approaches. (Again, these are much older techniques.) Solution: You can create a Java Date object to represent the current date in just a few lines of code, as shown in the following code:
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ how to get current date time
Java - How to get current date time - Mkyong.com
September 23, 2022 - package com.mkyong.app; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class ZonedDateTimeExample { public static void main(String[] args) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"); // Get default time zone System.out.println(ZoneOffset.systemDefault()); // Asia/Kuala_Lumpur System.out.println(OffsetDateTime.now().getOffset()); // +08:00 // get current date time, with +08:00 ZonedDateTime now = ZonedDateTime.now(); System.out.println(dtf.f
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ current-date-time
Java Program to Get Current Date/Time
September 6, 2023 - 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 = ...
๐ŸŒ
ZetCode
zetcode.com โ€บ java โ€บ currentdatetime
Java current date time - how to get current date time in Java
Java current date time tutorial shows how to use various Java classes to get current date and time in Java.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ get current date and time in java
Get Current Date and Time in Java - Scaler Topics
July 28, 2024 - Milliseconds, but if we want our date time in some different pattern, let's say if we want our month part before the year part, we can also achieve that using the DateTimeFormatter class, which will help us in getting the date time in our own pattern choice, and will give the result as 10-2022-04 21:09:50.407 which is of format Month-Year-Day Hours:Minutes: Seconds.Milliseconds. Hence, in short, we can get data and time in Java in different formats using different classes. Now let us look at an example of getting the current date and time in Java programming language, for more clear understanding.
๐ŸŒ
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 ...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ 7-ways-get-current-date-time-java-terala-chittibabu
7 ways to get current date & time in java
September 6, 2023 - Java 8 introduced LocalDate class in java.time package, which simplifies getting current date.