Period plusDays()方法在Java中


可以使用Java 中 Period 類的 plusDays() 方法獲取 Period 物件的一個不可變副本,其中添加了一些天。此方法需要一個引數,即要新增的天數,它返回帶有已新增天數的 Period 物件。

示例如下所示,展示了這一點

示例

 線上演示

import java.time.Period;
public class Demo {
   public static void main(String[] args) {
      String period = "P5Y7M15D";
      Period p1 = Period.parse(period);
      System.out.println("The Period is: " + p1);
      Period p2 = p1.plusDays(5);
      System.out.println("The Period after adding 5 days is: " + p2);
   }
}

輸出

The Period is: P5Y7M15D
The Period after adding 5 days is: P5Y7M20D

讓我們瞭解一下上面的程式。

首先顯示當前時間段。然後,使用 plusDays() 方法獲得 Period 的不可變副本,其中添加了 5 天,並顯示該副本。一個演示此程式碼片段如下

String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusDays(5);
System.out.println("The Period after adding 5 days is: " + p2);

更新於:2019 年 7 月 30 日

141 條瀏覽記錄

啟動你的 職業

完成課程即可獲得認證

開始
廣告