调用getTime更改日历值 [英] Calling getTime changes Calendar value

查看:42
本文介绍了调用getTime更改日历值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取给定日期的同一周的星期日。
在此期间,我遇到了这个问题:

I'm trying to get the sunday of the same week as a given date. During this I ran into this problem:

Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.set(2017, 11, 11);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println(calendar.getTime().toString());

结果为 CET 2018年1月7日11:18:42

results in "Sun Jan 07 11:18:42 CET 2018"

但是

Calendar calendar2 = Calendar.getInstance(Locale.GERMANY);
calendar2.set(2017, 11, 11);
calendar2.getTime();
calendar2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println(calendar2.getTime().toString());

为我提供正确的日期 CET 2017年12月17日11:18:42

gives me the correct Date "Sun Dec 17 11:18:42 CET 2017"

有人可以解释为什么第一个示例具有这种方式吗?

Can someone explain why the first exmple is behaving this way? Is this really intended?

谢谢

推荐答案

基本上,日历 API很可怕,应避免使用。它的记录还不是很清楚,但是我认为我知道它的去向,并且在这种情况下它的行为符合预期。意思是说,这是遵循API作者的意图,而不是您或任何阅读您代码的人的意图。

Basically, the Calendar API is horrible, and should be avoided. It's not documented terribly clearly, but I think I see where it's going, and it's behaving as intended in this situation. By that I mean it's following the intention of the API authors, not the intention of you or anyone reading your code...

来自文档


可以通过调用set方法来设置日历字段的值。在需要计算其时间值(距纪元为毫秒)或日历字段的值之前,不会解释日历中设置的任何字段值。调用get,getTimeInMillis,getTime,添加和滚动涉及这种计算。

The calendar field values can be set by calling the set methods. Any field values set in a Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.

然后:


从日历字段中计算日期和时间时,可能没有足够的信息来进行计算(例如仅年份和月份而没有月份),或者信息不一致(例如,7月星期二) 1996年5月15日(Gregorian)-1996年7月15日实际上是星期一)。日历将通过以下方式解析日历字段值以确定日期和时间。

When computing a date and time from the calendar fields, there may be insufficient information for the computation (such as only year and month with no day of month), or there may be inconsistent information (such as Tuesday, July 15, 1996 (Gregorian) -- July 15, 1996 is actually a Monday). Calendar will resolve calendar field values to determine the date and time in the following way.

如果日历字段值存在任何冲突,则日历会为最近设置的日历字段赋予优先级。以下是日历字段的默认组合。将使用由最近设置的单个字段确定的最新组合。

If there is any conflict in calendar field values, Calendar gives priorities to calendar fields that have been set more recently. The following are the default combinations of the calendar fields. The most recent combination, as determined by the most recently set single field, will be used.

对于日期字段:


  • YEAR + MONTH + DAY_OF_MONTH

  • YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

  • YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK

  • YEAR + DAY_OF_YEAR

  • YEAR + DAY_OF_WEEK + WEEK_OF_YEAR

  • YEAR + MONTH + DAY_OF_MONTH
  • YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
  • YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
  • YEAR + DAY_OF_YEAR
  • YEAR + DAY_OF_WEEK + WEEK_OF_YEAR

在第一个示例中,最后一个字段集是星期几这一事实得到了解决。表示它将使用 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK 计算(我认为)。年和月已设置为2017年12月,但月的周是当前的月,即2018年1月5日的周...所以当您说将星期几设置为周日,它将在第5周中找到周日到2017年12月为止。12月只有4周,因此有效地将其向前推...我认为。都杂乱无章,基本上您不必考虑这一点。

In the first example, the fact that the last field set was "day of week" means it will then use the YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK calculation (I think). The year and month have been set to December 2017, but the week-of-month is the current week-of-month, which is the week 5 of January 2018... so when you then say to set the day of week to Sunday, it's finding the Sunday in the "week 5" of December 2017. December only had 4 weeks, so it's effectively rolling it forward... I think. It's all messy and you shouldn't have to think about that, basically.

在第二个示例中,调用 getTime() ;锁定您指定的年/月/日,并计算其他字段。设置星期几后,便会在现有的计算字段中进行调整。

In the second example, calling getTime() "locks in" the year/month/day you've specified, and computes the other fields. When you set the day of week, that's then adjusting it within the existing computed fields.

基本上,尽可能避免使用此API。使用 java.time ,这是一个 far 更干净的日期/时间API。

Basically, avoid this API as far as you possibly can. Use java.time, which is a far cleaner date/time API.

这篇关于调用getTime更改日历值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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