java 判断两个日期 是否同一天
把二个时间的的年月日分别对比,完全相等就是同一天,代码下:
private static boolean isSameDate(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
.get(Calendar.YEAR);
boolean isSameMonth = isSameYear
&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
boolean isSameDate = isSameMonth
&& cal1.get(Calendar.DAY_OF_MONTH) == cal2
.get(Calendar.DAY_OF_MONTH);
return isSameDate;
}
public boolean isSameDay(Date day1, Date day2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String ds1 = sdf.format(day1);
String ds2 = sdf.format(day2);
if (ds1.equals(ds2)) {
return true;
} else {
return false;
}
}