📌  相关文章
📜  Java中 toSecondsPart() 方法的持续时间和示例

📅  最后修改于: 2022-05-13 01:55:31.498000             🧑  作者: Mango

Java中 toSecondsPart() 方法的持续时间和示例

使用Java.time 包Duration 类toSecondsPart()方法,通过除以一分钟的秒数,得到该持续时间的秒数。

句法:

public long toSecondsPart()

参数:此方法不接受任何参数。

返回值:此方法返回一个long 值,该值是此持续时间中的秒数除以一分钟的秒数。

下面的示例说明了 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–