概述
这样,可以在数天,数小时和数分钟内获得纳秒。首先,设置持续时间:Duration d1 = Duration.ofDays(5);
Duration d2 = Duration.ofHours(20);
Duration d3 = Duration.ofMinutes(15);
将以上持续时间转换为纳秒:System.out.println("Nanoseconds in 5 days = "+d1.toNanos());
System.out.println("Nanoseconds in 20 hours = "+d2.toNanos());
System.out.println("Nanoseconds in 15 minutes = "+d3.toNanos());
示例import java.time.Duration;
public class Demo {
public static void main(String[] args) {
Duration d1 = Duration.ofDays(5);
Duration d2 = Duration.ofHours(20);
Duration d3 = Duration.ofMinutes(15);
System.out.println("Nanoseconds in 5 days = "+d1.toNanos());
System.out.println("Nanoseconds in 20 hours = "+d2.toNanos());
System.out.println("Nanoseconds in 15 minutes = "+d3.toNanos());
}
}
输出结果Nanoseconds in 5 days = 432000000000000
Nanoseconds in 20 hours = 72000000000000
Nanoseconds in 15 minutes = 900000000000
最后
以上就是激昂路人为你收集整理的java 时间长度_Java程序将此持续时间转换为总长度(以纳秒为单位)的全部内容,希望文章能够帮你解决java 时间长度_Java程序将此持续时间转换为总长度(以纳秒为单位)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复