问题描述
我正在使用GXT / ExtGWT。我有下面的代码比较两个日期。
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;
}
在datefields fromdate中,我选择了 0012-12 -30
和todate作为 0012-12-31
。
Here in datefields fromdate I selected as 0012-12-30
and todate as 0012-12-31
.
当行 differenceBetweenDates.before(toDate)
被执行,我得到异常。请帮帮我。
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
推荐答案
据,java.util.Date包含以下代码:
According to http://www.docjar.com/html/api/java/util/Date.java.html, java.util.Date contains this code:
private static final BaseCalendar getCalendarSystem(long utc) {
// Quickly check if the time stamp given by `utc' is the Epoch
// or later. If it's before 1970, we convert the cutover to
// local time to compare.
if (utc >= 0
|| utc >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER
- TimeZone.getDefaultRef().getOffset(utc)) {
return gcal;
}
return getJulianCalendar();
}
因为你将年份设置为0012而不是2012年,它选择JulianCalendar。
So it looks to me that because you are putting year in as 0012 not 2012, it chooses JulianCalendar.
这篇关于比较日期时发生ClassCastException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!