Open In App

WeekFields of() method in Java with Examples

Last Updated : 29 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




    // 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);
        }
    }

    
    

    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:




    // 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);
        }
    }

    
    

    Output:

    WeekFields[SUNDAY, 1]
    

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads