0

So, I have to check if the difference between 2 dates is at least one day. Then the method should return true, otherwise it is false.

The format for dates that I´m using is GregorianCalendar(year, month, day).

So I already have a method that uses date1.before(date2), but that also checks the time, so ifdate1 is before date2 it returns true, even if they are the same day (but the time is different!).

What I need is a way to check if the difference between the dates is at least 1 day.

Any ideas?

user2440009
  • 13
  • 1
  • 4

2 Answers2

2

You can use something like this:

public boolean dateDifference(Date d1, Date d2)
{ 
long currentDateMilliSec = d1.getTime();
long updateDateMilliSec = d2.getTime();
long diffDays = (currentDateMilliSec - updateDateMilliSec) / (24 * 60 * 60 * 1000);
if (diffDays >= 1) return true;
else return false;
}
Oscerd
  • 1,616
  • 11
  • 14
1

Add 1 day to cal1 and test if it's not after cal2

    cal1.add(Calendar.DATE, 1);
    System.out.println(!cal1.after(cal2));
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275