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