how to get the beginning of a day using Gregorian Calendar and Date in java
-
30-05-2021 - |
Question
i am trying to mark date if the data created in the past 3 days. I want to take the last 3 days , from the beginning (00:00).
i have this code :
int days = 3
GregorianCalendar gc=new GregorianCalendar();
gc.add(GregorianCalendar.DAY_OF_MONTH, -days);
if (timeCreated.before(gc.getTime())) {
return true;
}
return false;
if the current time is 16:00 clock i get the past 3 days , from 16:00 clock to now. I want to get from 00:00 3 days ago till now.
Thanks in advanced.
Solution
set the hours, minutes and seconds in your calendar object to zero.
gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
gc.set(GregorianCalendar.MINUTE, 0);
gc.set(GregorianCalendar.SECOND, 0);
gc.set(GregorianCalendar.MILLISECOND,0);
Remember to do this AFTER you have called gc.add
.
OTHER TIPS
try with
gc.set(GregorianCalendar.HOUR_OF_DAY,0);
gc.set(GregorianCalendar.MINUTE,0);
gc.set(GregorianCalendar.SECOND,0);
gc.set(GregorianCalendar.MILLISECOND,0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow