1

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.

nquincampoix
  • 508
  • 1
  • 4
  • 17
Ofer
  • 4,879
  • 6
  • 20
  • 26
  • Set hours, minutes, seconds and milliseconds to zero on your calendar. An example is here: http://stackoverflow.com/questions/6850874/how-to-create-a-java-date-object-of-midnight-today-and-midnight-tomorrow – samlewis Apr 08 '12 at 08:33

2 Answers2

6

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.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
1

try with

gc.set(GregorianCalendar.HOUR_OF_DAY,0);
gc.set(GregorianCalendar.MINUTE,0);
gc.set(GregorianCalendar.SECOND,0);
gc.set(GregorianCalendar.MILLISECOND,0);
dash1e
  • 7,677
  • 1
  • 30
  • 35