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


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


時期類的withDays()方法用於獲取具有指定天數的時期。該天數作為參數傳遞為整數值。

用法:

public Period withDays(int numberOfDays)

參數:此方法接受參數numberOfDays,它是此期間要更改的天數。


返回值:此函數返回以傳遞的天數為參數的Period。

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

示例1:

// Java code to demonstrate withDays() method 
  
import java.time.Period; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the String to be withDaysd 
        String period = "P1Y2M21D"; 
  
        // Parse the String into Period 
        Period p = Period.parse(period); 
  
        System.out.println("Period: " + p); 
  
        // Get the number of days 
        int numberOfDays = 5; 
  
        // Change the numberOfDays of this Period 
        // using withDays() method 
        System.out.println("New Period: "
                           + p.withDays(numberOfDays)); 
    } 
}
輸出:
Period: P1Y2M21D
New Period: P1Y2M5D

示例2:

// Java code to demonstrate withDays() method 
  
import java.time.Period; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the String to be withDaysd 
        String period = "-P1Y2M21D"; 
  
        // Parse the String into Period 
        Period p = Period.parse(period); 
  
        System.out.println("Period: " + p); 
  
        // Get the number of days 
        int numberOfDays = -5; 
  
        // Change the numberOfDays of this Period 
        // using withDays() method 
        System.out.println("New Period: "
                           + p.withDays(numberOfDays)); 
    } 
}
輸出:
Period: P-1Y-2M-21D
New Period: P-1Y-2M-5D

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



相關用法


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