java.time.Period.plus(TemporalAmount amountToAdd)
方法返回此Period
的副本,并添加了指定的Period
。
声明
以下是java.time.Period.plus(TemporalAmount amountToAdd)
方法的声明。
public Period plus(TemporalAmount amountToAdd)
参数
amountToAdd
- 要添加的期间,正或负值,不为null
。
返回值
一个基于此Period
的期间,添加了指定的Period
,而不是null
。
异常
DateTimeException
- 如果指定的数量具有非ISO年表或包含无效单位。ArithmeticException
- 如果发生数字溢出。
示例
以下示例显示了java.time.Period.plus(TemporalAmount amountToAdd)
方法的用法。
package com.yiibai;
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