Get current date without time
12,241
Solution 1
You could use a Calendar
to solve this:
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date desiredDate = cal.getTime();
Solution 2
The best thing is joda-time if you can use it.
Otherwise, use Calendar API. call Calendar.set() to set hour, minute, second and millisecond to zero then you have a Date of the starting of the date.
But, won't new DateTime().withTimeAtStartOfDay() be a much easier expression?
Solution 3
In Java 8 you could do
LocalDate date = LocalDate.of(2014, Month.MARCH, 13);
Author by
MakoBuk
Updated on July 26, 2022Comments
-
MakoBuk almost 2 years
I need to solve following issue. I need to create date for comparison with other, but the date has to be without time due to comparing timemillis.
I have tried many ways, but still unsuccessfully.
I would imagine format like this:
Date(2014-13-03 0:00:00).getTimeMillis();
Do anyone know how?