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


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