简体   繁体   中英

Java's Calendar.DAY_OF_WEEK_IN_MONTH max value is 6. Is this correct?

As the title states, creating a java.util.GregorianCalendar object, let's say calendar, and running

calendar.getMaximum(Calendar.DAY_OF_WEEK_IN_MONTH)

it returns 6! To the best of my knowledge, this should be 5, since calendar.getMaximum(Calendar.DAY_OF_MONTH) equals 31 and 31 / 7 == 4 with a remainder, ie there are at most 5 weeks and thus a day can occur at most 5 times in a month.

Am I missing something here?

The javadoc states:

For example, if a month has 31 days, DAY_OF_WEEK_IN_MONTH -1 will overlap DAY_OF_WEEK_IN_MONTH 5 and the end of 4.

This admits, that the maximum value can be 5. In order to have a reason for value six, there would have to be a month with 36 days, which shouldn't happen even with date/time corrections. At least I haven't found anything like that checking information about Gregorian calendar.

So yes, the maximum value should be 5.


Edit: I've also checked the source of the reference implementation and there is no reason for value 6.

You're right, it makes no sense. For DAY_OF_WEEK_IN_MONTH to be 6 , there would have to be a month with 6 Mondays in it (for example).

The reason why its 6 not 5 is because in English calendar, start day for a week is Sunday. If, for example, A month's first day, let's say July 1st is Sunday, then July 2nd is already the next week. Since July has 31 days, you end up with 6 weeks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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