Java中的Period getDays()方法

可以使用getDays()Java的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 days are: " + p.getDays());
   }
}

输出结果

The Period is: P5Y7M15D
The number of days are: 15

现在让我们了解上面的程序。

首先显示期间。然后,使用该getDays()方法获得“期间”的天数并显示。演示此代码段如下所示:

String period = "P5Y7M15D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The number of days are: " + p.getDays());