Java Duration toNanosPart()方法及实例

Java Duration 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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java Duration