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


Java Period ofDays()用法及代碼示例


周期類的ofDays()方法用於從給定的天數中獲取一個周期作為參數。該參數以整數形式接受。此方法返回具有給定天數的Period。

用法:

public static Period ofDays(int numberOfDays)

參數:此方法接受單個參數numberOfDays,該參數是要解析為Period對象的天數。


返回值:此函數返回以給定天數解析的Period對象。

下麵是Period.ofDays()方法的實現:

示例1:

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

示例2:

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

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



相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Period ofDays() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。