當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java WeekFields of()用法及代碼示例


WeekFields類的of()方法可幫助我們獲取WeekFields的實例。

根據傳遞給它的參數,有兩種類型的of()方法。

  1. (DayOfWeek firstDayOfWeek,int minimumDaysInFirstWeek):此方法可幫助我們從第一個day-of-week到最少天數創建一個WeekFields實例。對於firstDayOfWeek和minimalDaysInFirstWeek的每個唯一組合,將返回相同的實例。

    用法:


    public static WeekFields of(DayOfWeek firstDayOfWeek, 
                              int minimalDaysInFirstWeek)
    

    參數:此方法接受兩個參數:

    • firstDayOfWeek這是一周的第一天。不能為空
    • minimalDaysInFirstWeek這是第一周的最少天數,從1到7。

    返回值:此方法返回week-definition,不為null。

    異常:如果最小日期的值小於一或大於7,則此方法將引發IllegalArgumentException。

    以下示例程序旨在說明WeekFields.of(DayOfWeek firstDayOfWeek,int minimumDaysInFirstWeek)方法:
    程序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); 
        } 
    }
    輸出:
    WeekFields[MONDAY, 1]
    
  2. 的(Locale locale):此方法可幫助我們獲取適合於語言環境的WeekFields實例。
    用法:
    public static WeekFields of(Locale locale)
    

    參數:此方法接受語言環境作為參數,這是要使用的語言環境。它不能為空。

    返回值:此方法返回week-definition,不為null。

    以下示例程序旨在說明WeekFields.of(long min,long maxSmallest,long maxLargest)方法:
    程序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); 
        } 
    }
    輸出:
    WeekFields[SUNDAY, 1]
    

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



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 WeekFields of() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。