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
广告
© . All rights reserved.