Java program to get the current date without timestamp

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.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.

Java Date/Calendar example: How to get today's date (now , and time; then use the getTime method to convert this to a java. Before Java 8, there wasn't a direct way to get a Date without time unless we were using third party libraries like Joda-time. This is because the Date class in Java is a representation of a specific instant in time, expressed in milliseconds. Therefore, this makes it impossible to ignore the time on a Date.

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) 

Get Date Without Time in Java, Take a look at how to get a Date value without the time in Java. THE unique Spring Security education if you're working with Java today. This is because the Date class in Java is a representation of a Let's see it in code:. LocalDate represents just a date, 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

Use DateFormat to solve this problem:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dateFormat2 = new SimpleDateFormat("MM-dd-yyyy");
print(dateFormat.format(new Date()); // will print like 2014-02-20
print(dateFormat2.format(new Date()); // will print like 02-20-2014

How to Get Current Date and Time in Java, our banks offer us the transaction list with the exact timestamps for us to review. And to do this in Java, we need only a couple of lines of code: In Java, getting the current date is as simple as instantiating the Date object from the Java This means that we can only get the current date, but without the time of the day: In Java, timestamps should be represented with java.time.Instant from Java 8, and java.sql.Timestamp till Java 7. Get current instant using 3 methods. 2 methods to get current timestamp. Drop questions in comments section. A family guy with fun loving nature. Love computers, programming and solving everyday problems.

I did as follows and it worked: (Current date without timestamp)

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date today = dateFormat.parse(dateFormat.format(new Date()));

How to get current date and time in java, In this tutorial we will see how to get the current date and time using Date and Calendar class and how to Complete java code for getting current date and time. Its quite easy to get the current timestamp in java. In this tutorial we will see how to get the timestamp using Date and Timestamp class. Here are the steps that we have followed in the below example: 1) Created the object of Date class. 2) Got the current time in milliseconds by calling getTime()

You can get by this date:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
print(dateFormat.format(new Date());

Java Date & Time: SimpleDateFormat, Current Date & Compare, Display Current Date in Java; SimpleDateFormat: Parse and Format Dates; Compare Dates Example. Let us first understand the parameters  There are many ways to get current date and time in java. There are many classes that can be used to get current date and time in java. The LocalDateTime.now () method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints current date and time. To format the current date, you can use

How to get current date and time in Python?, We will use the date class of the datetime module to accomplish this task. Example 1: Python get today's date. from datetime import date today = date.today​() print  After executing the above Java code you will get the following values in return. Time in Milli Seconds: 1447402821007 Current Time Stamp: 2015-11-13 13:50:21.007 Java Get Date Only. You can use SimpleDateFormat with Calendar class to find the date and time only.

Java Date - parse, format and conversion, Example of getting current date in Java. SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy/  You can create a “current time” JDBC Timestamp in just a few lines of code by using the Java Calendar class and a java.util.Date instance. I show this in the three steps of the following example code, where I (a) get a Calendar instance, (b) get a Date from that instance, and then (c) get a Timestamp instance from that Date :

How to format Date in Java, In this example we will see simple date formatting without time information e.g. dd​-MM-yyyy, Date This is how to get today's date in Java I need to format Date with Timestamp, which I am receiving as String, in format "25 JUL 2013 20:30". We can convert Date to Timestamp in java using constructor of java.sql.Timestamp class. The constructor of Timestamp class receives long value as an argument. So you need to convert date into long value using getTime () method of java.util.Date class. You can also format the output of Timestamp using java.text.SimpleDateFormat class.

Comments
  • I didn't know about cal.clear(), I'm going to steal it and update my answer.
  • You may if you give me +1 ;-)
  • This works, but you have to clear both HOUR and HOUR_OF_DAY to get a true clearing - i.e., setting of the time to 12am.
  • @WhyGeeEx (years after your comment, which may have been true then): Docs as of Java 7 clarify this as not necessary: If HOUR_OF_DAY is the most recent field set, then it takes precedence over "AM_PM + HOUR". docs.oracle.com/javase/7/docs/api/java/util/… "If there is any conflict in calendar field values, Calendar gives priorities to calendar fields that have been set more recently". Then it goes on to specifically show HOUR_OF_DAY vs "AM_PM + HOUR".
  • @ToolmakerSteve: Yep, 2010 was very pre-Java-7.
  • I read the documentation of isSameDay, and I don't get how it works. How can it determine the day without using the time zone? sometimes two Date instances can be either on the same day or on different days (before and after midnight) depending on the time zone being assumed. Does it assume the default timezone of the system?
  • I don't know exactly what it does. You'd have to look at the source to be sure. However, if you look at the Date API, it appears to do everything based on the local time zone. In fact, all of its methods relating to converting to other timezones are deprecated. So, really Date looks like it doesn't really deal with timezones. If you want to do anything with timezones, look at Calendar.
  • Date internally is milliseconds since "January 1, 1970, 00:00:00 GMT". I would expect day computations to also be in GMT.
  • On the other hand, I see that (new Date).toString() uses the default locale of current device (on Android anyway) to determine time zone. So isSameDay might do the same.
  • This is a simple way of getting the date in a format you want.
  • it gives timestamp also with date
  • I will not pass any date as u mentioned.
  • Updated the code to go through GregorianCalendar.getInstance() which returns current date.
  • Why use GregorianCalendar explicitly - why not just use Calendar.getInstance()?