Java 中的 Duration toNanos() 方法
在 Java 中,可以使用 Duration 类中的 toNanos() 方法获得特定持续时间与纳秒数量之间的值。此方法不要求任何参数,它以纳秒数量返回持续时间。
演示此过程的程序如下 −
示例
import java.time.Duration;
public class Demo {
public static void main(String[] args) {
Duration d = Duration.ofSeconds(1);
System.out.println("The duration is: " + d);
System.out.println("The number of nanoseconds in the duration is: " + d.toNanos());
}
}输出
The duration is: PT1S The number of nanoseconds in the duration is: 1000000000
现在让我们理解上述程序。
首先显示持续时间。然后使用 toNanos() 方法获得该持续时间与纳秒数量之间的值并显示。演示此过程的代码片段如下 −
Duration d = Duration.ofSeconds(1);
System.out.println("The duration is: " + d);
System.out.println("The number of nanoseconds in the duration is: " + d.toNanos());
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP