I would like to know the best way to compare two Dates in Java , such as in this following scenario:
Date date = new Date(time);
Date now = new Date();
//
long day = 86400000L;
long days = day * 7;
if ((date.getTime() + days) <= now.getTime()) {
// DO 1
} else {
// DO 2
}
In other words: I would like to get a Date object, add in some days and compare them.
Question: Is there a good solution for Java 6? And for Java 8 (using the new Time API)? I would like to use the Date.after(Date date) and Date.before(Date date) for this (after adding the days to a Date)
Thank you!
EDIT
Looks like this can be done, according to Coffee. I dont know if there's a better way.
Calendar date = Calendar.getInstance();
date.setTime(new Date(time));
date.add(Calendar.DATE, 7);
Calendar now = Calendar.getInstance();
//
if(now.after(date)) {
// DO 1
} else {
// DO 2
}