java.time.Period.plusYears() 方法

描述

java.time.Period.plusYears(long years) 方法返回此 Period 的副本,其中添加了指定的 Period in years。


声明

以下是 java.time.Period.plusYears(long years) 方法的声明。

public Period plusYears(long years)

参数

years − 要添加的年份,正数或负数。


返回值

基于此 Period 的 Period 加上指定的年份,不为空。


异常

ArithmeticException − 如果发生数字溢出。


示例

下面的例子展示了 java.time.Period.plusYears(long years) 方法的使用。

package com.tutorialspoint;

import java.time.Period;

public class PeriodDemo {
   public static void main(String[] args) {

      Period period = Period.ofYears(2);
      Period period1 = period.plusYears(1);
      System.out.println(period1.getYears());   
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

3