java day of week,Java日历 - 设置day_of_week后日期无法预测

这篇博客探讨了Java中Calendar类在设置日期和星期时出现的冲突问题。当设置月份、日期和星期后,实际输出的日期不正确。原因是Calendar在设置时不立即更新所有字段,直到强制计算时才会解决冲突。插入get操作可以强制更新内部状态,从而得到预期结果。此外,地区设置的不同也可能影响到一周的起始日,导致不同结果。
摘要由CSDN通过智能技术生成

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));

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));

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?

解决方案

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

or

YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

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

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.

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.

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.

Edit: 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:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值