I only found a solution for Joda Time.
My solution works only if the last day is not in the first week:
LocalDate.now() // or any other LocalDate
.withDayOfMonth(31)
.withMonth(12)
.get(weekFields.weekOfWeekBasedYear())
So what is the correct way in Java Time (like in Joda Time)?
If one wants to get the week number based on 7 days no matter when the week starts and how many days the first partial week of the year has,
ChronoField.ALIGNED_WEEK_OF_YEAR
might be helpful.For example, the 1st of January 2016 based on the ISO-8601 definition (where a week starts on Monday and the first week has a minimum of 4 days) falls into week number 0, but in the aligned it is week number 1.
The correct and best solution is given by @JodaStephen. Here are some alternatives anyways.
December, 28th is always in the last week of a year, because the remaining three days after can not form a major part of another week:
A year has 53 weeks if it starts or ends with a thursday:
And finally this will give you the "week number" of the last day of year. It's 53 also in cases where the last week's number is 52 iff the major part of the last day's week lies in the next year (the week is claimed by the next year).
That's what you actually did.
It seems that when the last day is in the first week, you don't want to get
1
as an answer but52/3/4
, in which case you may be looking for:There are several ways to define week numbers - if that doesn't do what you want you need to clarify which method you want to use.
This information is available directly using the
java.time.*
API.The key method is
rangeRefinedBy(Temporal)
onTemporalField
. It allows you to obtain aValueRange
object that provides the minimum and maximum values for the field, refined by the temporal object passed in.To find out how many ISO weeks there are in the year, do the following:
Note that the date you pass in is used to determine the answer. So when passing in dates in early January or late December ensure you understand how the ISO week-based calendar works, and the difference between the calendar year and the week-based year.