借助此功能,可以获取以天,小时和分钟为单位的毫秒数。首先,设置持续时间-
Duration d1 = Duration.ofDays(20); Duration d2 = Duration.ofHours(100); Duration d3 = Duration.ofMinutes(150);
将上述持续时间转换为纳秒-
System.out.println("Milliseconds in 20 days = "+d1.toMillis()); System.out.println("Milliseconds in 100 hours = "+d2.toMillis()); System.out.println("Milliseconds in 150 minutes = "+d3.toMillis());
import java.time.Duration; public class Demo { public static void main(String[] args) { Duration d1 = Duration.ofDays(20); Duration d2 = Duration.ofHours(100); Duration d3 = Duration.ofMinutes(150); System.out.println("Milliseconds in 20 days = "+d1.toMillis()); System.out.println("Milliseconds in 100 hours = "+d2.toMillis()); System.out.println("Milliseconds in 150 minutes = "+d3.toMillis()); } }
输出结果
Milliseconds in 20 days = 1728000000 Milliseconds in 100 hours = 360000000 Milliseconds in 150 minutes = 9000000