How to set time of java.util.Date instance to 00:00:00?
date, java
Solution
To remove time from the `Date` object completely, you could use this:
public static Date removeTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
Pass into the method the `Date` object that you want to modify and it will return a `Date` that has no hours/minutes etc.
If changing the Date object itself isn't required, use `SimpleDateFormat`. Set the format the way you want ie. remove hours/minutes. Then call the format method and pass in the `Date` object you want changed.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy");
System.out.println(sdf.format(yourDate));
Problem
I have a variable of the type `java.util.Date`. How can I set the time part to 00:00:00? I am not allowed to use an Apache Commons library or JodaTime. The `java.util.Calendar` is probably my only option.