这样,可以在数天,数小时和数分钟内获得纳秒。首先,设置持续时间:
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