Java中的 Duration toDays() 方法及示例
使用Java.time 包中Duration 类的toDays()方法获取该持续时间的天数值。
句法:
public long toDays()
参数:此方法不接受任何参数。
返回值:此方法返回一个长值,即此持续时间中的天数。
下面的示例说明了 Duration.toDays() 方法:
示例 1:
// Java code to illustrate toDays() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration using parse() method
Duration duration
= Duration.parse("P2DT3H4M");
System.out.println("Duration: "
+ duration);
// Get the number of days
// using toDays() method
System.out.println(duration.toDays());
}
}
输出:
Duration: PT51H4M
2
示例 2:
// Java code to illustrate toDays() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration using ofDays() method
Duration duration
= Duration.ofDays(10);
System.out.println("Duration: "
+ duration);
// Get the number of days
// using toDays() method
System.out.println(duration.toDays());
}
}
输出:
Duration: PT240H
10
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/Duration.html#toDays–