How to set Date to midnight in Java

Here is piece of the code that will help you with setting the Date to midnight in Java

     public Date setDateToMidnight(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();

	}

We take the Calender instance and set the time to the user date. After that we set the hour, minute, second and millis to 0.

That gives us the Date set to midnight.

Leave a Comment