java.time.Period.plusMonths() 方法示例



說明

java.time.Period.plusMonths(long months) 方法返回此週期加上指定週期的週期副本(按月)。

宣告

以下是 java.time.Period.plusMonths(long months) 方法的宣告。

public Period plusMonths(long months)

引數

months - 要新增的月份,為正數或負數。

返回值

基於此週期加上指定週期的週期,不為 null。

異常

ArithmeticException - 如果發生數字溢位。

示例

以下示例展示了 java.time.Period.plusMonths(long months) 方法的使用方法。

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.ofMonths(2);
      Period period1 = period.plusMonths(1);
      System.out.println(period1.getMonths());   
   }
}

讓我們編譯並執行以上程式,將得到以下結果 -

3
廣告