Java 中的 Period getMonths() 方法
可以使用 Java 中 Period 类的 getMonths() 方法来获取特定 Period 的月份数。 此方法不需要参数,它返回 Period 中的月份数。
以下是一个演示此方法的程序
示例
import java.time.Period;
public class Demo {
public static void main(String[] args) {
String period = "P5Y7M15D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The number of months are: " + p.getMonths());
}
}输出
The Period is: P5Y7M15D The number of months are: 7
现在让我们了解一下上面的程序。
首先显示 Period。 然后使用 getMonths() 方法获取 Period 的月份数并显示出来。 以下代码片段演示了这个过程
String period = "P5Y7M15D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The number of months are: " + p.getMonths());
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP