Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

calendar

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, i.e. there are at most 5 weeks and thus a day can occur at most 5 times in a month.

Am I missing something here?

like image 270
user2435660 Avatar asked May 30 '13 08:05

user2435660


2 Answers

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.

like image 172
Mifeet Avatar answered Nov 15 '22 00:11

Mifeet


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

like image 39
RichieHindle Avatar answered Nov 14 '22 23:11

RichieHindle