- java.time 包類
- java.time - 首頁
- java.time - 時鐘
- java.time - 時段
- java.time - 時間戳
- java.time - 日期
- java.time - 日期時間
- java.time - 時間
- java.time - 月日
- java.time - 偏移日期時間
- java.time - 偏移時間
- java.time - 時段
- java.time - 年
- java.time - 年月
- java.time - 時間日期
- java.time - 時區識別符號
- java.time - 時區偏移量
- java.time 包列舉
- java.time - 月
- java.time 有用資源
- java.time - 討論
java.time.Period.plus() 方法示例
描述
java.time.Period.plus(TemporalAmount amountToAdd) 方法返回此 Period 的副本,添加了指定的 Period。
宣告
以下是 java.time.Period.plus(TemporalAmount amountToAdd) 方法的宣告。
public Period plus(TemporalAmount amountToAdd)
引數
amountToAdd − 要新增的 Period,正或負,不能為空。
返回值
基於此 Period 的 Period,加上指定的 Period,不能為空。
異常
DateTimeException − 如果指定的時間量有非 ISO 年代或包含無效的單位。
ArithmeticException − 如果發生數字溢位。
示例
以下示例演示了 java.time.Period.plus(TemporalAmount amountToAdd) 的用法。
package com.tutorialspoint;
import java.time.Period;
public class PeriodDemo {
public static void main(String[] args) {
Period period = Period.of(1,5,2);
System.out.println("Years: " + period.getYears()
+ ", Months: " + period.getMonths()
+", Days: " + period.getDays());
Period period1 = period.plus(Period.ofDays(5));
System.out.println("Years: " + period1.getYears()
+ ", Months: " + period1.getMonths()
+", Days: " + period1.getDays());
}
}
讓我們編譯並執行上述程式,這會產生以下結果 −
Years: 1, Months: 5, Days: 2 Years: 1, Months: 5, Days: 7
廣告