I need a Java program to get the current date without a timestamp:
Date d = new Date();
gives me date and timestamp.
But I need only the date, without a timestamp. I use this date to compare with another date object that does not have a timestamp.
On printing
System.out.println("Current Date : " + d)
of d it should print May 11 2010 - 00:00:00.
A
java.util.Dateobject 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 standardDateobject 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
Calendarand clear the hour, minutes, seconds and milliseconds:Do the same with another
Calendarobject that contains the date that you want to compare it to, and use theafter()orbefore()methods to do the comparison.As explained into the Javadoc of java.util.Calendar.clear(int field):
edit – The answer above is from 2010; in Java 8, there is a new date and time API in the package
java.timewhich is much more powerful and useful than the oldjava.util.Dateandjava.util.Calendarclasses. Use the new date and time classes instead of the old ones.