0
我试图比较日期以找到与当前日期(今天的日期)相距最近的两个日期。但是,我没有使用Date.before()获得预期的结果。Date1.before(Date2)未返回预期结果
代码在这里:
Date currentDate;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
public Date getDate(){
currentDate = new Date();
//Toast.makeText(Happenings.this, currentDate, Toast.LENGTH_SHORT).show();
return currentDate;
}
public String[] compareDate(Date currentDate){
String[] closestDates = new String[2];
//Toast.makeText(Happenings.this, sdf.format(currentDate), Toast.LENGTH_SHORT).show();
try {
Date date1 = sdf.parse("20/01");
Date date2 = sdf.parse("09/08");
Date date3 = sdf.parse("24/12");
Date[] availableDates = {date1, date2, date3}; // dd/mm
//Toast.makeText(Happenings.this, sdf.format(date1), Toast.LENGTH_SHORT).show();
if (currentDate.before(date3)) {
Toast toast = Toast.makeText(Happenings.this, "Before Festival", Toast.LENGTH_SHORT);
toast.show();
}
}
catch (ParseException e){
Toast.makeText(Happenings.this, "Catch", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return closestDates;
}
我现在我得到的是currentDate.before(DATE3)计算为false和currentDate.after(DATE3)评估为true。这与08/11之前的预期不同,应该在24/12之前。