当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。