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

2019-10-16 22:35:59

java.time.YearMonth.plus(long amountToAdd,TemporalUnit unit)方法返回此年月的副本,並新增了指定的數量。

宣告

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

public YearMonth plus(long amountToAdd, TemporalUnit unit)

引數

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

返回值

基於此日期的YearMonth,新增了指定的總數,而不是null

例外

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

範例

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

package com.yiibai;

import java.time.YearMonth;
import java.time.temporal.ChronoUnit;

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

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

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

2027-12