Java 8 Date & Time API: How to get ISO8601 number of weeks in a given month?

Now I am just recalculating the number of Thursdays from the 1st to the last date. Are there any linear solutions using the Java 8 Date and Time API? I tried:



but it gives the wrong result, for example on March 5,2014 it returns 5 while Marsh has only 4 weeks according to ISO8601.


source to share

1 answer

Lets ask a question like "how many Thursdays is this month"?

(ISO-8601 does not describe an approach over several weeks for several months - it only describes an approach over several weeks over several years).

We can use an approach to find the fifth appearance on Thursday, and see if it's in the same month:

LocalDate date = ...
LocalDate fifthThu = date.with(TemporalAdjusters.dayOfWeekInMonth(5, THURSDAY));
int thursdays = (fifthThu.getMonth() == date.getMonth() ? 5 : 4);




All Articles