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

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

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

Java.time 包Duration 类toNanosPart()方法用于获取持续时间秒内的纳秒部分。

句法:

public long toNanosPart()

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

返回值:此方法返回一个长值,它是持续时间秒内的纳秒部分。

下面的示例说明了 Duration.toNanosPart() 方法:

示例 1:

// Java code to illustrate toNanosPart() 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 nano-seconds
        // using toNanosPart() method
        System.out.println(duration.toNanosPart());
    }
}
输出:
Duration: PT51H4M
0

示例 2:

// Java code to illustrate toNanosPart() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using ofNanos() method
        Duration duration = Duration.ofNanos(10);
  
        System.out.println("Duration: "
                           + duration);
  
        // Get the number of nano-seconds
        // using toNanosPart() method
        System.out.println(duration.toNanosPart());
    }
}
输出:
Duration: PT0.00000001S
10

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/Duration.html#toNanosPart–