java.time包中的Duration Class的toNanos()方法用于获取此持续时间的值(以纳秒为单位)。
用法:
public long toNanos()
参数:此方法不接受任何参数。
返回值:此方法返回一个long值,该值是此持续时间中的纳秒数。
以下示例说明了Duration.toNanos()方法:
示例1:
// Java code to illustrate toNanos() 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 toNanos() method
System.out.println(duration.toNanos());
}
}
输出:
Duration: PT51H4M 183840000000000
示例2:
// Java code to illustrate toNanos() 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 toNanos() method
System.out.println(duration.toNanos());
}
}
输出:
Duration: PT0.00000001S 10
参考: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#toNanos–
相关用法
- Java TimeUnit toNanos()用法及代码示例
- Java Duration compareTo(Duration)用法及代码示例
- Java Duration dividedBy(Duration)用法及代码示例
- Java Duration equals(Duration)用法及代码示例
- Java Duration minus(Duration)用法及代码示例
- Java Duration plus(Duration)用法及代码示例
- Java Duration abs()用法及代码示例
- Java Duration toMinutesPart()用法及代码示例
- Java Duration toHoursPart()用法及代码示例
- Java Duration toSecondsPart()用法及代码示例
- Java Duration toMillisPart()用法及代码示例
- Java Duration toNanosPart()用法及代码示例
- Java Duration from(TemporalUnit)用法及代码示例
- Java Duration toDays()用法及代码示例
- Java Duration get(TemporalUnit)用法及代码示例
注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Duration toNanos() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。