Java日历 - 设置day_of_week后日期无法预测 [英] Java Calendar - Date is unpredictable after setting day_of_week

查看:126
本文介绍了Java日历 - 设置day_of_week后日期无法预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JUnit测试中有以下代码,上周似乎工作失败:

I have the following code in a JUnit test, which seemed to work last week is failing this week:

Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.JULY, 12);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); // push the date to 15
System.out.println(cal.get(Calendar.DATE));

你可以从我的评论中推断,因为12日是星期二,我希望Date是将DAY_OF_WEEK设置为星期五后15。但是,打印的值为22,导致测试失败。

As you could probably infer from my comment, since the 12th is a Tuesday, I expect Date to be 15 after setting the DAY_OF_WEEK to Friday. However, the value that is printed is 22, and causes the test to fail.

如果我按如下方式更改代码,并添加一个额外的调用来获取:

If I, however change the code as follows, and add an additional call to get:

Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.JULY, 12);
System.out.println(cal.get(Calendar.DATE));
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); // push the date to 15
System.out.println(cal.get(Calendar.DATE));

我得到了我期望的输出,12和15。

I get the output that I expect, 12 and 15.

有人可以解释一下发生了什么,为什么这个测试上周有效?

Can someone explain what is going on, and why this test was working last week?

推荐答案

第一个要理解的是,月+日+ DayOfWeek对日历没有任何意义。日历将根据

The first thing to understand is that Month + Day + DayOfWeek does not mean anything to the Calendar. The Calendar will calculate the true value of the date based on


年+月+日期

YEAR + MONTH + DATE


年+月+ WEEK_OF_MONTH + DAY_OF_WEEK

YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

(或其他一些组合,如年份+一年中的某一天等)所以Date + DayOfWeek本身并不意味着很多。

(Or some other combos like year + day of year etc.) So Date + DayOfWeek doesn't inherently mean much to it.

要理解的第二件事是,当你设置Java日历时,它实际上不会重新计算绝对时间或更新相关字段,直到强制计算的操作发生。

The second thing to understand is when you set on a Java Calendar it doesn't actually recompute the absolute time or update related fields until an operation that forces computation occurs.

第一次设置后,日历处于冲突状态。月份和日期说它是7月12日,但是月份周和星期几仍然说现在是今天,无论今天是什么。然后,您将周日设置为星期五。所以今天的月份和日期说是7月12日,但是一个月的一周和一周的一天字段表示它是'这个'周的星期五。

After your first set, the calendar is in a conflicted state. The month and day say that it's July 12th, but the 'week of month' and 'day of week' still say that it's today, whatever today is. You then call set day of week to friday. So now year month and day say July 12th, but the 'week of month' and 'day of week' fields say it's Friday of 'this' week.

规则日历表示最近设置的字段在发生冲突时获胜,因此本周五结合的星期和星期几用于计算其他字段。

The rules of the calendar say that the most recently set field "wins" when there's a conflict, so the week of month and day of week combining to say Friday of this week are what's used to calculate the other fields.

在中间插入一个修复它,因为它强制将日历的整个内部状态重新计算到7月12日星期二,然后再设置为星期五,因此没有内部冲突。通过在您将星期几设置为星期五之前重新计算,将月中的一周设置为包含7月12日的一周。

Inserting a get in the middle 'fixes' it because it forces the entire internal state of the calendar to get recomputed to Tuesday July 12th before setting to Friday, so there are no internal conflicts. The 'week of month' got set to the week that contains July 12th by the recalculation prior to you setting day of week to Friday.

编辑:很抱歉在进行更改之后两天,在一个旧的浏览器选项卡中注意到这一点,并认为我会扩展为未来的googlers的希望帮助:

Sorry to make changes after two days, noticed this open in an old browser tab and thought I would expand for the hopeful help of future googlers:

它在评论中为Jon工作的原因是他住在伦敦。他的电脑认为星期一星期开始。所以当被问到'这个'周的星期五时,7月17日星期日被问到时,它仍然在7月15日回复。我提出这个问题是因为在不同的Locales中,一周的第一天不同只是另一种尝试在日历中使用WEEK_OF字段的方式变得混乱。

The reason it worked for Jon in the comments is he lives in London. His computer thinks weeks start on Mondays. So when asked for Friday of 'this' week, it still responded July 15th when asked on Sunday July 17th. I bring this up because differing first days of the week in different Locales are just yet another way that trying to use the WEEK_OF fields in a calendar goes haywire.

这篇关于Java日历 - 设置day_of_week后日期无法预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆