GregorianCalendar 的奇怪行为

2022-01-11 00:00:00 calendar java

我刚刚在 GregorianCalendar 类中遇到了一个奇怪的行为,我想知道我是否真的在做坏事.

I just encountered a strange behaviour with the GregorianCalendar class, and I was wondering if I really was doing something bad.

仅当初始化日期的月份的实际最大值大于我要将日历设置为的月份时才会附加.

This only appends when the initialization date's month has an actualMaximum bigger than the month I'm going to set the calendar to.

这里是示例代码:

    // today is 2010/05/31  
    GregorianCalendar cal = new GregorianCalendar();

    cal.set(Calendar.YEAR, 2010);
    cal.set(Calendar.MONTH, 1); // FEBRUARY

    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));

    return cal.getTime(); // => 2010/03/03, wtf

我知道问题是由于日历初始化日期是一个 31 天的月份(可能),这与设置为 2 月(28 天)的月份混淆了.修复很容易(只需在设置 year 和 month 之前将 day_of_month 设置为 1 ),但我想知道这真的是想要的行为.有什么想法吗?

I know the problem is caused by the fact that the calendar initialization date is a 31 day month ( may ), which mess with the month set to february (28 days). The fix is easy ( just set day_of_month to 1 before setting year and month ), but I was wondering is this really was the wanted behaviour. Any thoughts ?

推荐答案

获取当前日期/时间的实际最大值.5 月有 31 天,比 2 月 28 日多 3 天,因此将移至 3 月 3 日.

It is getting the actual maximums of the current date/time. May has 31 days which is 3 more than 28 February and it will thus shift to 3 March.

您需要调用 Calendar#clear() 获取/创建后:

You need to call Calendar#clear() after obtaining/creating it:

GregorianCalendar cal = new GregorianCalendar();
cal.clear();
// ...

这会导致:

Sun Feb 28 23:59:59 GMT-04:00 2010

(根据我的时区,这是正确的)

(which is correct as per my timezone)

正如其中一个答案所说,java.util.CalendarDate 是史诗般的失败.在进行密集的日期/时间操作时考虑 JodaTime.

As said in one of the answers, the java.util.Calendar and Date are epic failures. Consider JodaTime when doing intensive date/time operations.

相关文章