Why does Java Calendar say the first Thursday of the month is week 5?

I'm trying to figure out how to set an alarm on a specific day of the week. Here is an example of 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 that I do not 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 on a specific day for a specific week. Date today is June 15, 2013. Can anyone explain this to me. Time in Joda is not an option of what I am doing, so I need to do this work with regular Java libraries. Thanks for the help.

Editor's Note

The above code will return different results depending on the launch date, since Calendar.getInstance() returns the calendar that is currently set. To illustrate a problem that does not depend on 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 locale uses the Gregorian calendar by default):

 Jun 15, 2013 Jun 13, 2013 Week of month: 5 May 30, 2013 
+4
source share
1 answer

Java Calendar is reduced by default. If you install it before January 32, it will translate it by February 1. In addition, the definition of "week" is local and a bit confusing. This is explained in detail in the JavaDocs associated with the above.

In your particular case (or at least in my Locale) the week is defined as the start on Sunday, and the β€œfirst week of June” is defined as the period from Sunday to Saturday, which includes June 1, June 1, 2013, this is Saturday, last day of the week 22 2013. Sunday, June 2, 2013, is the first day of the week of June 2, and not the second day of the week.

Since there is no Thursday in the week of June 1, the soft calendar interprets DAY_OF_WEEK, THURSDAY as the Thursday of the week of year 22 of 2013, which is May 30, 2013, which is the week of May 5, so you get 5 and not 1 at your exit.

To set the first Thursday of June, you want:

  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 little confusing. You use it with DAY_OF_WEEK to indicate which appearance of this day in this month you want. Therefore, when DAY_OF_WEEK set on 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 than a local or open (erroneous) interpretation, and then the days make up the first week of the month.

+5
source

Source: https://habr.com/ru/post/1486374/


All Articles