WeekFields of() method in Java with Examples

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.

  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // WeekFields.of(DayOfWeek, int) method
      
    import java.time.DayOfWeek;
    import java.time.temporal.WeekFields;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create WeekFields
            WeekFields weekFields
                = WeekFields.of(DayOfWeek.MONDAY, 1);
      
            // print results
            System.out.println(weekFields);
        }
    }

    chevron_right

    
    

    Output:

    WeekFields[MONDAY, 1]
    
  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // of(Locale locale) method
      
    import java.time.temporal.WeekFields;
    import java.util.Locale;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            Locale locale = new Locale("EN", "US");
      
            // create WeekFields
            WeekFields weekFields = WeekFields.of(locale);
      
            // print results
            System.out.println(weekFields);
        }
    }

    chevron_right

    
    

    Output:

    WeekFields[SUNDAY, 1]
    

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/WeekFields.html

java-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.