java.time.Year.plus(long amountToAdd, TemporalUnit unit)方法

2019-10-16 22:36:51

java.time.Year.plus(long amountToAdd,TemporalUnit unit)方法返回指定數量的今年副本。

宣告

以下是java.time.Year.plus(long amountToAdd,TemporalUnit unit)方法的宣告。

public Year plus(long amountToAdd, TemporalUnit unit)

引數

  • amountToAdd - 要新增到結果中的單位數量可能為負數。
  • unit - 要新增的金額的單位,而不是null

返回值

基於此日期的年份,新增了指定的金額,而不是null

異常

  • DateTimeException - 如果無法新增。
  • UnsupportedTemporalTypeException - 如果不支援該單位。
  • ArithmeticException - 如果發生數位溢位。

範例

以下範例顯示了java.time.Year.plus(long amountToAdd,TemporalUnit unit)方法的用法。

package com.yiibai;

import java.time.Year;
import java.time.temporal.ChronoUnit;

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

      Year date = Year.parse("2017");
      Year date1 = date.plus(10, ChronoUnit.YEARS);
      System.out.println(date1);  
   }
}

編譯並執行上面的程式,這將產生以下結果 -

2027