【发布时间】:2021-11-18 14:14:48
【问题描述】:
public boolean checkDates(String oldDate, String newDate){
java.util.Calendar oldCal = java.util.Calendar.getInstance();
java.util.Calendar newCal = java.util.Calendar.getInstance();
String[] oD = oldDate.split("-");
String[] nD = newDate.split("-");
oldCal.set(Integer.parseInt(oD[0]), Integer.parseInt(oD[1])-1, Integer.parseInt(oD[2]));
newCal.set(Integer.parseInt(nD[0]), Integer.parseInt(nD[1])-1, Integer.parseInt(nD[2]));
if(newCal.compareTo(oldCal) > 0){
return true;
}else {
return false;
}
这就是我正在实施的代码,它在同一天的日期有不同的结果。有时 compareTo 提供 1 有时是 0。比较日期的示例打印:
oldCal Fri Nov 17 23:30:48 CST 2021
newCal Fri Nov 17 23:30:48 CST 2021
返回 1 和返回 0 的所有时间都相同。
【问题讨论】:
-
不要再使用过时的日历 API。换成新的 java.time api。
-
oldCal 和 newCal 的毫秒数可能不同
-
您是否手动使用旧的日历 API 解析日期?现在是 2021 年,时间到 get with the program。哦等等,这是安卓。但它肯定不会卡在日历上(或笨拙的手动解析)。
-
我认为你说得对 D Blacksmith with Calendar...以前没有使用过 LocalDate 但现在...生活很美好。