I'm trying to compare a time with the time 24 hours ago.
This is what I have:
public boolean inLastDay(Date aDate) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
Date pastDay = cal.getTime();
if(aDate!= null) {
if(aDate.after(pastDay)){
return true;
} else {
return false;
}
} else {
return false;
}
}
An example of input (these are converted from Strings to Dates):
null (this would return false)
Jul 11 at 19:36:47 (this would return false)
Jul 14 at 19:40:20 (this would return true)
This doesn't seem to be working. It always returns false. Any help would be appreciated!
Answer: In the end I kept getting false because "aDate" had no milliseconds, year, and other values that "pastDay" did.
To fix this I did the following:
SimpleDateFormat sdfStats = new SimpleDateFormat("MMM dd 'at' HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
Date yesterdayUF = cal.getTime();
String formatted = sdfStats.format(yesterdayUF);
Date yesterday = null;
try {
yesterday = sdfStats.parse(formatted);
} catch (Exception e) {
}
if(aDate!= null) {
if(aDate.after(yesterday)){
return true;
} else {
return false;
}
} else {
return false;
}