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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。