- 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
广告