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


Java Period ofWeeks()用法及代码示例


周期类的ofWeeks()方法用于从给定的周数中获取一个周期作为参数。该参数以整数形式接受。此方法返回具有给定周数的Period。

用法:

public static Period ofWeeks(int numberOfWeeks)

参数:此方法接受单个参数numberOfWeeks,该参数是要解析为Period对象的Weeks数。


返回值:此函数返回以给定的周数解析的Period对象。

下面是Period.ofWeeks()方法的实现:

示例1:

// Java code to demonstrate ofWeeks() method 
  
import java.time.Period; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the number of Weeks 
        int numberOfWeeks = 5; 
  
        // Parse the numberOfWeeks into Period 
        // using ofWeeks() method 
        Period p = Period.ofWeeks(numberOfWeeks); 
  
        System.out.println(p.getYears() + " Years\n"
                           + p.getMonths() + " Months\n"
                           + p.getDays() + " Days"); 
    } 
}
输出:
0 Years
0 Months
35 Days

示例2:

// Java code to demonstrate ofWeeks() method 
  
import java.time.Period; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the number of Weeks 
        int numberOfWeeks = -5; 
  
        // Parse the numberOfWeeks into Period 
        // using ofWeeks() method 
        Period p = Period.ofWeeks(numberOfWeeks); 
  
        System.out.println(p.getYears() + " Years\n"
                           + p.getMonths() + " Months\n"
                           + p.getDays() + " Days"); 
    } 
}
输出:
0 Years
0 Months
-35 Days

参考: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#ofWeeks-int-



相关用法


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