简体   繁体   English

为什么Java Calendar会说本月的第一个星期四是第5周?

[英]Why is Java Calendar saying the first Thursday of the month is Week 5?

I am trying to figure out how to make an alarm on a specific day of the week. 我想弄清楚如何在一周的特定日期发出警报。 Here is sample code that prints what I expect: 这是打印我期望的示例代码:

  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.WEEK_OF_MONTH, 1);
  System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Week of month: 1

But when I add this to the code I get a result I dont understand: 但是,当我将其添加到代码中时,我得到的结果我不明白:

  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  calendar.set(Calendar.WEEK_OF_MONTH, 1);
  System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Week of month: 5

I am trying to set an alarm for a specific day in a specific week. 我试图在特定的一周内为特定日期设置警报。 The date today is June 15th, 2013. Could someone please explain this to me. 今天的日期是2013年6月15日。有人可以向我解释一下。 Joda time is not an option with what I am doing, so I need to make this work with the regular Java libs. Joda时间不是我正在做的选择,所以我需要使用常规Java库来完成这项工作。 Thanks for all the help. 谢谢你的帮助。

Editor's note 编者注

The above code will return different results based on the date it is run, because Calendar.getInstance() returns a Calendar set to the current time. 上面的代码将根据它运行的日期返回不同的结果,因为Calendar.getInstance()返回一个设置为当前时间的Calendar。 For an illustration of the problem that is independent of the current time, use this: 有关与当前时间无关的问题的说明,请使用以下命令:

DateFormat dateFormat = DateFormat.getDateInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1371294000000L);
System.out.println(dateFormat.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
System.out.println(dateFormat.format(calendar.getTime()));
calendar.set(Calendar.WEEK_OF_MONTH, 1);
System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); 
System.out.println(dateFormat.format(calendar.getTime()));

Which outputs something like this (at least if your default locale is using the Gregorian Calendar): 哪个输出这样的东西(至少如果您的默认语言环境使用公历):

Jun 15, 2013
Jun 13, 2013
Week of month: 5
May 30, 2013

Java Calendar is lenient by default. 默认情况下,Java Calendar宽松的。 If you set it to January 32, it will translate that to Feb 1. Furthermore, the definition of "week" is Locale-specific and a bit confusing. 如果将其设置为1月32日,则会将其转换为2月1日。此外,“周”的定义是特定于区域设置的,有点令人困惑。 This is explained in great detail in the JavaDocs linked to above. 这在上面链接的JavaDocs中有详细解释。

In your particular case (or at least in my Locale), a week is defined as starting on Sunday and the "first week of June" is defined as the Sunday through Saturday period that includes June 1. June 1, 2013, is a Saturday, the last day in week 22 of the year 2013. Sunday, June 2, 2013, is the first day of week 2 of June, not the second day of week 1. 在您的特定情况下(或至少在我的区域设置中),一周被定义为从星期日开始,“六月的第一周”被定义为包括6月1日的星期日到星期六的时间段。2013年6月1日是星期六,2013年第22周的最后一天。2013年6月2日星期日,是6月第2周的第一天,而不是第1周的第二天。

Since there is no Thursday in week 1 of June, the lenient calendar interprets DAY_OF_WEEK, THURSDAY to be the Thursday of week 22 of year 2013, which is May 30, 2013, which is week 5 of May, so you get 5 and not 1 in your output. 由于6月第1周没有星期四,因此宽松日历将DAY_OF_WEEK, THURSDAY解释为2013年第22周的星期四,即2013年5月30日,即5月的第5周,因此您获得5而不是1在你的输出中。

To set the first Thursday in June, you want: 要设置6月的第一个星期四,您需要:

 Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.MONTH, Calendar.JUNE);
 calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
 calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);

DAY_OF_WEEK_IN_MONTH can be a bit confusing. DAY_OF_WEEK_IN_MONTH可能有点令人困惑。 You use it with DAY_OF_WEEK to specify which occurrence of that day in that month you want. 您可以将它与DAY_OF_WEEK一起使用,以指定您想要的那一天中哪一天。 So when DAY_OF_WEEK is set to Thursday, DAY_OF_WEEK_IN_MONTH, 1 is the first Thursday of the month, DAY_OF_WEEK_IN_MONTH, 2 is the second Thursday of the month, etc. This is much less Locale-specific or open to (mistaken) interpretation then what days constitute the first week of the month. 因此,当DAY_OF_WEEK设置为星期四时, DAY_OF_WEEK_IN_MONTH, 1是该月的第一个星期四, DAY_OF_WEEK_IN_MONTH, 2是该月的第二个星期四,等等。这远远少于特定于语言环境或开放(错误)解释然后是什么日子构成这个月的第一周。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM