java.time.Period.withMonths() 方法
描述
java.time.Period.withMonths(int months) 方法返回此 Period 的副本,其中包含指定的月份。
声明
以下是 java.time.Period.withMonths(int months) 方法的声明。
public Period withMonths(int months)
参数
months − 代表的月份,可能是负数。
返回值
基于此期间的 Period 与请求的月份,不为空。
示例
下面的例子展示了 java.time.Period.withMonths(int months) 方法的使用。
package com.tutorialspoint;
import java.time.Period;
public class PeriodDemo {
public static void main(String[] args) {
Period period = Period.ofMonths(2);
System.out.println(period.toString());
period = period.withMonths(5);
System.out.println(period.toString());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
P2M P5M

