Java Duration toSecondsPart()方法及示例
java.time包中Duration类的toSecondsPart()方法用于通过除以一分钟内的秒数来获得该持续时间的秒数值。
语法。
public long toSecondsPart()
参数。此方法不接受任何参数。
返回值。该方法返回一个长值,即该持续时间中的秒数,除以一分钟的秒数。
下面的例子说明了Duration.toSecondsPart()方法。
例1:
// Java code to illustrate toSecondsPart() 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 seconds
// using toSecondsPart() method
System.out.println(duration.toSecondsPart());
}
}
输出:
Duration: PT51H4M
0
例2:
// Java code to illustrate toSecondsPart() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration using ofSeconds() method
Duration duration
= Duration.ofSeconds(10);
System.out.println("Duration: "
+ duration);
// Get the number of seconds
// using toSecondsPart() method
System.out.println(duration.toSecondsPart());
}
}
输出:
Duration: PT10S
10
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#toSecondsPart-