The of() method of WeekFields class helps us to obtain an instance of WeekFields.
There are two types of of() methods based on parameters passed to it.
- of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek): This method helps us to an instance of WeekFields from the first day-of-week and minimal days.WeekFields instances are singletons; for each unique combination of firstDayOfWeek and minimalDaysInFirstWeek the same instance will be returned.
Syntax:
public static WeekFields of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek)Parameters: This method accepts two parameters:
- firstDayOfWeek which is the first day of the week. It should not be null
- minimalDaysInFirstWeek which is the minimal number of days in the first week, from 1 to 7.
Return value: This method returns the week-definition, not null.
Exception: This method throws IllegalArgumentException if the minimal day’s value is less than one or greater than 7.
Below program illustrate the WeekFields.of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) method:
Program 1:// Java program to demonstrate// WeekFields.of(DayOfWeek, int) methodÂÂimportjava.time.DayOfWeek;importjava.time.temporal.WeekFields;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// create WeekFields       ÂWeekFields weekFields           Â= WeekFields.of(DayOfWeek.MONDAY,1);       Â// print results       ÂSystem.out.println(weekFields);   Â}}Output:WeekFields[MONDAY, 1]
- of(Locale locale): This method help us to get an instance of WeekFields appropriate for a locale.
Syntax:public static WeekFields of(Locale locale)
Parameters: This method accepts locale as a parameter which is the locale to use. It should not be null.
Return value: This method returns the week-definition, not null.
Below program illustrate the WeekFields.of(long min, long maxSmallest, long maxLargest) method:
Program 2:// Java program to demonstrate// of(Locale locale) methodÂÂimportjava.time.temporal.WeekFields;importjava.util.Locale;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       ÂLocale locale =newLocale("EN","US");       Â// create WeekFields       ÂWeekFields weekFields = WeekFields.of(locale);       Â// print results       ÂSystem.out.println(weekFields);   Â}}Output:WeekFields[SUNDAY, 1]
References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/WeekFields.html
