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
WeekFieldsThe java.time classes already support non-standard weeks. You may not need to build your own class. An implementation of
java.time.Temporalwith the necessaryTemporalFieldobjects has been provided in theWeekFieldsclass.The
WeekFields.ISOconstant uses standard ISO 8601 definition of a week:But that class is built for alternative week definitions as well. The constant
WeekFields.SUNDAY_STARTcomes configured for United States-style Sunday-Monday weeks.Get the week number and week-based-year number:
If you define a week differently, configure
WeekFieldsas 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.YearWeekclass from ThreeTen-Extra. Such a class would contain theWeekFieldsobject shown above, and would offer methods wrapping the code seen above. The source-code forYearWeek.javais available with a liberal license.