Java 中的 Period plusMonths() 方法
添加了部分月份后,Period 对象的不可变副本可以通过使用 Java 中的 Period 类的 plusMonths() 方法获得。此方法需要一个参数,即要添加的月份数,它返回已添加了月份的 Period 对象。
下面提供了一个演示此方法的程序
实例
import java.time.Period;
public class Demo {
public static void main(String[] args) {
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusMonths(2);
System.out.println("The Period after adding 2 months is: " + p2);
}
}输出
The Period is: P5Y7M15D The Period after adding 2 months is: P5Y9M15D
现在,让我们了解一下上述程序。
首先显示当前 Period。然后,使用 plusMonths() 方法获取添加了 2 个月的 Period 的不可变副本,并进行显示。演示此方法的代码片段如下
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.plusMonths(2);
System.out.println("The Period after adding 2 months is: " + p2);
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP