2

I am trying to remove the entries in my database that have Date (java.util.Date) older than 10 days of the current date. Is there any way to compare just the "day" value inside the Date. Not just comparing which Date value is greater, but actually making sure there is X day in between the two Date values.

mirage1s7
  • 277
  • 1
  • 5
  • 10

2 Answers2

6

Say you have your two Date values as time1 and time2.

int daysApart = (int)((time2.getTime() - time1.getTime()) / (1000*60*60*24l));
if (abs(daysApart) >= 10) 
    System.out.println("10+ days apart.");
else
    System.out.println("Less than 10 days apart.");
krodmannix
  • 845
  • 10
  • 30
0

Just create a new Date object for current date + 10 days and compare to that.

i.e.

new Date(System.currentTimeMillis()+10L*24*60*60*1000);

As the comments say though the Date objects are pretty much obsolete now, you should look to using one of the more current time approaches.

Tim B
  • 40,716
  • 16
  • 83
  • 128