I need an object that represents a localized concept of a seven days week. That object is pretty much the same as the YearWeek
found in the ThreeTen-Extra library, except that the first day and the minimal number of days varies.
I thought initially to implement a LocalizedYearWeek
, with some factory methods such as LocalizedYearWeek.of(int year, int week, WeekDefinition weekDefinition)
. The class WeekDefinition
is merely a value object that defines the first day and the minimal number of days of a week, that can also work as a factory of WeekFields
.
I'm not sure if it is the best model for such temporal, but I didn't come up with any better design ideas, so I'm looking for some guidance on how to implement it.
tl;dr
WeekFields
The java.time classes already support non-standard weeks. You may not need to build your own class. An implementation of
java.time.Temporal
with the necessaryTemporalField
objects has been provided in theWeekFields
class.The
WeekFields.ISO
constant uses standard ISO 8601 definition of a week:But that class is built for alternative week definitions as well. The constant
WeekFields.SUNDAY_START
comes configured for United States-style Sunday-Monday weeks.Get the week number and week-based-year number:
If you define a week differently, configure
WeekFields
as needed.If you want to represent a year-week as a single unit, then yes, you will need to write your own class similar to
org.threeten.extra.YearWeek
class from ThreeTen-Extra. Such a class would contain theWeekFields
object shown above, and would offer methods wrapping the code seen above. The source-code forYearWeek.java
is available with a liberal license.