Reputation: 12621
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
Upvotes: 1
Views: 2559
Reputation: 1706
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:
private static boolean isBefore(Date firstDate, Date secondDate) {
return firstDate.getTime() < secondDate.getTime();
}
Upvotes: 1
Reputation: 1406
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();
}
So it looks to me that because you are putting year in as 0012 not 2012, it chooses JulianCalendar.
Upvotes: 3
Reputation: 66886
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.
Upvotes: 0