比较日期时出现 ClassCastException?

发布于 2024-12-26 05:33:33 字数 992 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为守护你 2025-01-02 05:33:33

根据 http://www.docjar.com/html/api /java/util/Date.java.html, java.util.Date 包含以下代码:

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。

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.

如此安好 2025-01-02 05:33:33

我有同样的问题,但针对不同的用例(日期确实来自用户输入,但数据源是 Excel 表)。

一个非常简单的解决方法对我来说很有效:

private static boolean isBefore(Date firstDate, Date secondDate) {
    return firstDate.getTime() < secondDate.getTime();
}

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();
}
柳絮泡泡 2025-01-02 05:33:33

您的日期解析有问题。有人认为您打算根据凯撒儒略历来表示时间!除非你是东正教徒,否则我怀疑这就是意图。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文