Java 中的 Period plusMonths() 方法
添加了部分月份後,Period 物件的不可變副本可以透過使用 Java 中的 Period 類的 plusMonths() 方法獲得。此方法需要一個引數,即要新增的月份數,它返回已添加了月份的 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.plusMonths(2);
System.out.println("The Period after adding 2 months is: " + p2);
}
}輸出
The Period is: P5Y7M15D The Period after adding 2 months is: P5Y9M15D
現在,讓我們瞭解一下上述程式。
首先顯示當前 Period。然後,使用 plusMonths() 方法獲取添加了 2 個月的 Period 的不可變副本,並進行顯示。演示此方法的程式碼片段如下
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusMonths(2);
System.out.println("The Period after adding 2 months is: " + p2);
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP