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


Java WeekFields weekOfWeekBasedYear()用法及代码示例


WeekFields类的weekOfWeekBasedYear()方法用于返回一个字段,以基于此WeekFields访问week-based-year的星期。例:

  • 如果一年的第一天是星期一,则第一周从第一天开始
  • 如果一年的第二天是星期一,则第一周从第二天开始,第一周在上一年的最后一周
  • 如果一年的第4天是星期一,则第1周从第4周开始,而第1至第3周在上一年的最后一周
  • 如果一年中的第5天是星期一,则第二周从第5周开始,第1至第4周在第1周

该字段可用于任何日历系统。

用法:


public TemporalField weekOfWeekBasedYear()

参数:此方法不接受任何内容。

返回值:此方法返回一个字段,该字段提供对week-of-week-based-year的访问,不为null。

以下示例程序旨在说明WeekFields.weekOfWeekBasedYear()方法:
程序1:

// Java program to demonstrate 
// WeekFields.weekOfWeekBasedYear() 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 weekOfWeekBasedYear() 
        TemporalField weekOfWeekBasedYear 
            = weekFields.weekOfWeekBasedYear(); 
  
        // create a LocalDate 
        LocalDate day = LocalDate.of(2021, 12, 21); 
  
        // get week of week for localdate 
        int wow = day.get(weekOfWeekBasedYear); 
  
        // print results 
        System.out.println("Week of week for "
                           + day + ":" + wow); 
    } 
}
输出:
Week of week for 2021-12-21:52

程序2:

// Java program to demonstrate 
// WeekFields.weekOfWeekBasedYear() 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 weekOfWeekBasedYear() 
        TemporalField weekOfWeekBasedYear 
            = weekFields.weekOfWeekBasedYear(); 
  
        // create a LocalDate 
        LocalDate day = LocalDate.of(2014, 10, 12); 
  
        // get week of week for localdate 
        int wow = day.get(weekOfWeekBasedYear); 
  
        // print results 
        System.out.println("Week of week for "
                           + day + ":" + wow); 
    } 
}
输出:
Week of week for 2014-10-12:42

参考:https://docs.oracle.com/javase/10/docs/api/java/time/temporal/WeekFields.html#weekOfWeekBasedYear()



相关用法


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