WeekFields類的weekOfYear()方法用於返回一個字段,以基於此WeekFields訪問一年中的星期。例:
- 如果一年的第一天是星期一,則第一周從第一天開始,沒有零周
- 如果一年的第二天是星期一,則第一周從第二天開始,而第一天在零周
- 如果一年中的第4天是星期一,則第1周從第4周開始,第1至第3周在零周開始
- 如果一年中的第5天是星期一,則第二周從第5周開始,第1至第4周在第1周
該字段可用於任何日曆係統。
用法:
public TemporalField weekOfYear()
參數:此方法不接受任何內容。
返回值:此方法返回一個提供對week-of-year的訪問權限的字段,而不是null。
以下示例程序旨在說明WeekFields.weekOfYear()方法:
程序1:
// Java program to demonstrate
// WeekFields.weekOfYear() method
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);
// apply weekOfYear()
TemporalField weekOfYear
= weekFields.weekOfYear();
// create a LocalDate
LocalDate day
= LocalDate.of(2021, 12, 21);
// get week of year for localdate
int wow = day.get(weekOfYear);
// print results
System.out.println("Week of year for "
+ day + ":" + wow);
}
}
輸出:
Week of year for 2021-12-21:52
程序2:
// Java program to demonstrate
// WeekFields.weekOfYear() method
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);
// apply weekOfYear()
TemporalField weekOfYear
= weekFields.weekOfYear();
// create a LocalDate
LocalDate day = LocalDate.of(2014, 10, 12);
// get week of year for localdate
int wow = day.get(weekOfYear);
// print results
System.out.println("Week of year for "
+ day + ":" + wow);
}
}
輸出:
Week of year for 2014-10-12:42
參考:https://docs.oracle.com/javase/10/docs/api/java/time/temporal/WeekFields.html#weekOfYear()
相關用法
- Java WeekFields of()用法及代碼示例
- Java WeekFields getMinimalDaysInFirstWeek()用法及代碼示例
- Java WeekFields getFirstDayOfWeek()用法及代碼示例
- Java WeekFields hashCode()用法及代碼示例
- Java WeekFields toString()用法及代碼示例
- Java WeekFields dayOfWeek()用法及代碼示例
- Java WeekFields weekBasedYear()用法及代碼示例
- Java WeekFields equals()用法及代碼示例
- Java WeekFields weekOfMonth()用法及代碼示例
- Java WeekFields weekOfWeekBasedYear()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 WeekFields weekOfYear() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。