java.time.Period.withMonths(int months)
方法返回具有指定月份的此Period
的副本。
声明
以下是java.time.Period.withMonths(int months)
方法的声明。
public Period withMonths(int months)
参数
months
- 表示月份,可能是负值的。
返回值
基于此期间的请求月份的期间,不为null
。
示例
以下示例显示了java.time.Period.withMonths(int months)
方法的用法。
package com.yiibai;
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