Period 中的 minusMonths() 方法在 Java 中
在 Java 中,可以使用 Period 类中的 minusMonths() 方法获取 Period 对象的不可变副本(从副本中减去某些月份)。此方法需要一个参数,即要减去的月份数,它返回减去月份的 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.minusMonths(2);
System.out.println("The Period after subtracting 2 months is: " + p2);
}
}输出
The Period is: P5Y7M15D The Period after subtracting 2 months is: P5Y5M15D
现在,让我们了解一下上面的程序。
首先显示当前周期。然后,使用 minusMonths() 方法获取 Period 的不可变副本,其中减去 2 个月,并显示。演示此方法的代码片段如下
String period = "P5Y7M15D";
Period p1 = Period.parse(period);
System.out.println("The Period is: " + p1);
Period p2 = p1.minusMonths(2);
System.out.println("The Period after subtracting 2 months is: " + p2);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP