I'm trying to compare the current date(2016-08-31) with given date (2016-08-31). My current mobile device time zone is GMT-08:00 Pacific Time.
If I disable automatic date & time zone on device and set time zone as GMT+08:00 Perth, method1 will return true but method2 returns false;
The result of method2 is expected since I compare the date without time zone, so "2016-08-31" before "2016-08-31" is false; Why method1 returns true?
public boolean method1() {
try {
GregorianCalendar currentCalendar = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date endDate = sdf.parse("2016-08-31");
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);
if (endCalendar.before(currentCalendar)) {
return true;
} else {
return false;
}
} catch (ParseException e) {
...
}
}
public boolean method2() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = sdf.parse(formatter.format(new Date()));
Date endDate = sdf.parse("2016-08-31");
if (endDate.before(currentDate)) {
return true;
} else {
return false;
}
} catch (ParseException e) {
...
}
}