比较日期时出现 ClassCastException?
我正在使用 GXT/ExtGWT。我有下面的代码比较两个日期。
private DateField startDateField = new DateField();
private DateField endDateField = new DateField();
Date date = new Date();
CalendarUtil.addDaysToDate(date, -1);
startDateField.setValue(date);
endDateField.setValue(new Date());
Date fromDate = startDateField.getValue();
Date toDate = endDateField.getValue();
Date differenceBetweenDates = new Date(fromDate.getTime());
CalendarUtil.addMonthsToDate(differenceBetweenDates, 6);
if (differenceBetweenDates.before(toDate)) {
MessageBox.alert("Alert","Date range should not exceed six months", null);
return false;
} else{
return true;
}
在日期字段中,我选择的 fromdate 为 0012-12-30
,todate 为 0012-12-31
。
当执行 differenceBetweenDates.before(toDate)
行时,出现以下异常。请帮我。我在这里做错了吗?
java.lang.ClassCastException: sun.util.calendar.JulianCalendar$Date cannot be cast to sun.util.calendar.Gregorian$Date
I am using GXT/ExtGWT. I have below code which compares two dates.
private DateField startDateField = new DateField();
private DateField endDateField = new DateField();
Date date = new Date();
CalendarUtil.addDaysToDate(date, -1);
startDateField.setValue(date);
endDateField.setValue(new Date());
Date fromDate = startDateField.getValue();
Date toDate = endDateField.getValue();
Date differenceBetweenDates = new Date(fromDate.getTime());
CalendarUtil.addMonthsToDate(differenceBetweenDates, 6);
if (differenceBetweenDates.before(toDate)) {
MessageBox.alert("Alert","Date range should not exceed six months", null);
return false;
} else{
return true;
}
Here in datefields fromdate I selected as 0012-12-30
and todate as 0012-12-31
.
When the line differenceBetweenDates.before(toDate)
is executed, I am getting below exception. Please help me. Am i doing any wrong here?
java.lang.ClassCastException: sun.util.calendar.JulianCalendar$Date cannot be cast to sun.util.calendar.Gregorian$Date
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 http://www.docjar.com/html/api /java/util/Date.java.html, java.util.Date 包含以下代码:
}
所以在我看来,因为您将年份输入为 0012 而不是 2012,所以它选择 JulianCalendar。
According to http://www.docjar.com/html/api/java/util/Date.java.html, java.util.Date contains this code:
}
So it looks to me that because you are putting year in as 0012 not 2012, it chooses JulianCalendar.
我有同样的问题,但针对不同的用例(日期确实来自用户输入,但数据源是 Excel 表)。
一个非常简单的解决方法对我来说很有效:
I have the same problem, but for a different use case (the Dates do come from user input, yet the data source is an Excel table).
A very simple workaround did the trick for me:
您的日期解析有问题。有人认为您打算根据凯撒儒略历来表示时间!除非你是东正教徒,否则我怀疑这就是意图。
endDateField
内部存在一些问题,即使用完全错误的日历操作日期。Your date parsing has some problem. Something thinks you intend to represent time according to Caesar's Julian calendar! Unless you're Orthodox I doubt this is the intent.
endDateField
has some problem inside that is manipulating dates with the entirely wrong calendar.