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


Java java.time.temporal.WeekFields用法及代码示例


WeekFields类代表星期的定义,提供TemporalField实例。 WeekFields 提供五个字段:weekOfYear()、weekOfMonth()、dayOfWeek()、weekOfWeekBasedYear() 和 weekBasedYear(),它们提供对任何时间对象的值的访问。

每个不同的 WeekFields 都需要一个单独的 Field 实例,并且需要:

  • 一周开始
  • 最少天数

类声明:

public final class WeekFields
extends Object
implements Serializable

WeekFields类继承以下方法 来自 java.lang.Object 类:

  • clone()
  • finalize()
  • getClass()
  • notify()
  • notifyAll()
  • wait()

WeekFields类的方法:

方法 说明
dayOfWeek() 此方法返回一个字段,用于根据此 WeekFields 访问星期几。
equals(Object object) 此方法检查此WeekFields是否等于指定的对象。
getFirstDayOfWeek() 此方法获取第一个day-of-week。
getMinimalDaysInFirstWeek() 此方法在第一周获得最少的天数。
hashCode() 此方法返回此 WeekFields 的哈希代码。
of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) 此方法从第一个 day-of-week 和最少天数中获取 WeekFields 的实例。
of(Locale locale) 此方法获取适合区域设置的 WeekFields 实例。
toString() 此方法获取此 WeekFields 实例的字符串表示形式。
weekBasedYear() 此方法返回一个字段,用于根据此 WeekFields 访问 week-based-year 的年份。
weekOfMonth() 此方法返回一个字段,用于根据此 WeekFields 访问月份中的第几周。
weekOfWeekBasedYear() 此方法返回一个字段,用于根据此 WeekFields 访问 week-based-year 的周。
weekOfYear() 此方法返回一个字段,用于根据此 WeekFields 访问一年中的第几周。

示例 1:

Java


// Java program to demonstrate 
// WeekFields class 
  
import java.time.DayOfWeek; 
import java.time.LocalDate; 
import java.time.temporal.TemporalField; 
import java.time.temporal.WeekFields; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create WeekFields 
        WeekFields weekFields 
            = WeekFields.of(DayOfWeek.MONDAY, 1); 
  
        // creating separate temporal fields for each method 
  
        // apply dayOfWeek() 
        TemporalField dayOfWeek = weekFields.dayOfWeek(); 
  
        // apply weekBasedYear() 
        TemporalField weekBasedYear 
            = weekFields.weekBasedYear(); 
  
        // apply weekOfMonth() 
        TemporalField weekOfMonth 
            = weekFields.weekOfMonth(); 
  
        // apply weekOfWeekBasedYear() 
        TemporalField weekOfWeekBasedYear 
            = weekFields.weekOfWeekBasedYear(); 
  
        // create a LocalDate 
        LocalDate day = LocalDate.of(2021, 03, 31); 
  
        // get day of week for localdate 
        int dow = day.get(dayOfWeek); 
  
        // get week based year for localdate 
        int wby = day.get(weekBasedYear); 
  
        // get week of month for localdate 
        int wom = day.get(weekOfMonth); 
  
        // get week of week for localdate 
        int wow = day.get(weekOfWeekBasedYear); 
  
        // print results 
  
        System.out.println("day of week for " + day + " :"
                           + dow); 
        System.out.println("week based year for " + day 
                           + " :" + wby); 
        System.out.println("week of month for " + day + " :"
                           + wom); 
        System.out.println("Week of week for " + day + " :"
                           + wow); 
    } 
}
输出
day of week for 2021-03-31 :3
week based year for 2021-03-31 :2021
week of month for 2021-03-31 :5
Week of week for 2021-03-31 :14

示例 2:

Java


// Java program to demonstrate 
// WeekFields class 
  
import java.time.DayOfWeek; 
import java.time.LocalDate; 
import java.time.temporal.TemporalField; 
import java.time.temporal.WeekFields; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create WeekFields 
        WeekFields weekFields 
            = WeekFields.of(DayOfWeek.SUNDAY, 1); 
  
        // creating separate temporal fields for each method 
  
        // apply dayOfWeek() 
        TemporalField dayOfWeek = weekFields.dayOfWeek(); 
  
        // apply weekBasedYear() 
        TemporalField weekBasedYear 
            = weekFields.weekBasedYear(); 
  
        // apply weekOfMonth() 
        TemporalField weekOfMonth 
            = weekFields.weekOfMonth(); 
  
        // apply weekOfWeekBasedYear() 
        TemporalField weekOfWeekBasedYear 
            = weekFields.weekOfWeekBasedYear(); 
  
        // create a LocalDate 
        LocalDate day = LocalDate.of(2021, 12, 05); 
  
        // get day of week for localdate 
        int dow = day.get(dayOfWeek); 
  
        // get week based year for localdate 
        int wby = day.get(weekBasedYear); 
  
        // get week of month for localdate 
        int wom = day.get(weekOfMonth); 
  
        // get week of week for localdate 
        int wow = day.get(weekOfWeekBasedYear); 
  
        // print results 
  
        System.out.println("day of week for " + day + " :"
                           + dow); 
        System.out.println("week based year for " + day 
                           + " :" + wby); 
        System.out.println("week of month for " + day + " :"
                           + wom); 
        System.out.println("Week of week for " + day + " :"
                           + wow); 
    } 
}
输出
day of week for 2021-12-05 :1
week based year for 2021-12-05 :2021
week of month for 2021-12-05 :2
Week of week for 2021-12-05 :50


相关用法


注:本文由纯净天空筛选整理自surbhityagi15大神的英文原创作品 java.time.temporal.WeekFields Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。