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);
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP