简体   繁体   中英

how to get the beginning of a day using Gregorian Calendar and Date in java

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.

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 .

try with

gc.set(GregorianCalendar.HOUR_OF_DAY,0);
gc.set(GregorianCalendar.MINUTE,0);
gc.set(GregorianCalendar.SECOND,0);
gc.set(GregorianCalendar.MILLISECOND,0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM