Do you absolutely have to use java.util.Date? I would thoroughly recommend that you use Joda Time or the java.time package from Java 8 instead. In particular, while Date and Calendar always represent a particular instant in time, with no such concept as "just a date", Joda Time does have a type representing this (LocalDate). Your code will be much clearer if you're able to use types which represent what you're actually trying to do.

There are many, many other reasons to use Joda Time or java.time instead of the built-in java.util types - they're generally far better APIs. You can always convert to/from a java.util.Date at the boundaries of your own code if you need to, e.g. for database interaction.

Answer from Jon Skeet on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java dates โ€บ get date without time in java
Get Date Without Time in Java | Baeldung
March 12, 2025 - In the next sections, weโ€™ll show some common workarounds to tackle this problem. One of the most common ways to get a Date without time is to use the Calendar class to set the time to zero.
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ datetime โ€บ java-datetime-exercise-40.php
Java - Display current date, time without time, date
import java.time.LocalDate; import ... args){ LocalDate l_date = LocalDate.now(); System.out.println("Current date: " + l_date); LocalTime l_time = LocalTime.now(); System.out.println("Current time: " + l_time); } } ... N.B.: ...
๐ŸŒ
W3Docs
w3docs.com โ€บ java
How do I get a Date without time in Java?
To get a java.util.Date object with the time set to 00:00:00 (midnight), you can use the toInstant() method to convert a LocalDate object to an Instant, and then use the atZone() method to convert the Instant to a ZonedDateTime object, and finally use the toLocalDate() method to extract the LocalDate from the ZonedDateTime.
๐ŸŒ
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
LocalDateTime.now() gets the current date and time without time zone information. LocalDate.now() gets only the current date. LocalTime.now() gets only the current time. ZonedDateTime.now() gets the current date and time with time zone information.
Top answer
1 of 16
39

A java.util.Date object is a kind of timestamp - it contains a number of milliseconds since January 1, 1970, 00:00:00 UTC. So you can't use a standard Date object to contain just a day / month / year, without a time.

As far as I know, there's no really easy way to compare dates by only taking the date (and not the time) into account in the standard Java API. You can use class Calendar and clear the hour, minutes, seconds and milliseconds:

Calendar cal = Calendar.getInstance();
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.AM_PM);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

Do the same with another Calendar object that contains the date that you want to compare it to, and use the after() or before() methods to do the comparison.

As explained into the Javadoc of java.util.Calendar.clear(int field):

The HOUR_OF_DAY, HOUR and AM_PM fields are handled independently and the the resolution rule for the time of day is applied. Clearing one of the fields doesn't reset the hour of day value of this Calendar. Use set(Calendar.HOUR_OF_DAY, 0) to reset the hour value.

edit - The answer above is from 2010; in Java 8, there is a new date and time API in the package java.time which is much more powerful and useful than the old java.util.Date and java.util.Calendar classes. Use the new date and time classes instead of the old ones.

2 of 16
11

You could always use apache commons' DateUtils class. It has the static method isSameDay() which "Checks if two date objects are on the same day ignoring time."

static boolean isSameDay(Date date1, Date date2) 
๐ŸŒ
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 the java.time.LocalDate, uses LocalDate.now() to get the current date without a time-zone, and format it with the DateTimeFormatter.
Find elsewhere
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ how_do_i_get_a_date_without_time_in_java
How do I get a Date without time in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
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 - LocalTime is the opposite of LocalDate in that it represents only a time, without the date. This means that we can only get the current time of the day, without the actual date:
๐ŸŒ
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()
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());
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ java-get-current-date-time
How to get current date and time in Java
October 14, 2022 - As the name suggests, the LocalDate class stores the date in the ISO-8601 format (yyyy-MM-dd) without any time or timezone information. This means that you can only get the current date in the system's default timezone without time.
๐ŸŒ
Tutorjoes
tutorjoes.in โ€บ Java_example_programs โ€บ get_date_before_and_after_1year_compares_to_current_date_in_java
Write a Java program to get a date before and after 1 year compares to the current date
Display Current Date without Time and Current Time without Date in Java ยท Calculate the Difference Between Two Dates in Days in Java ยท Get Seconds since 1970 in Java ยท Convert a Unix Timestamp to Date in Java ยท Extract Date and Time from the Date String in Java ยท
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ languages โ€บ getting current date time in java
Getting Current Date Time in Java
October 16, 2015 - This is an immutable object that represent a time without a date and a time-zone in the ISO-8601 calendar system. The output of the code is. -----Current time of your time zone----- Current time of the day: 23:13:01.762 ยท In the output above, observe that the time is represented to nanosecond precision. The java.time package provides classes that you can use to get the current time of a different time zone.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-print-current-time-of-the-system-without-date-in-Java
How to print current time of the system without date in Java - Quora
Answer (1 of 2): Try this. If d[3] doesnt print it, try d[4] or d[2] [code]public static void main(String args[]) { java.util.Date date = new java.util.Date(); String[] d = date.toString().split("\\s+"); System.out.println(d[3]); } [/code]
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ how-to-get-the-current-date-time-in-java
How to Get Current Date/Time in Java: Best Practices and Methods Explained โ€” javathinking.com
LocalTime currentTimeInParis = ... // e.g., 16:30:45.123456789 (if UTC is 14:30) LocalDateTime combines LocalDate and LocalTime to represent a date and time without a time zone....
๐ŸŒ
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 } }
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - import java.time.LocalDateTime; LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("Current Date and Time: " + currentDateTime); Explain Code ยท This code snippet retrieves the current date and time using the LocalDateTime class.