s-day01-Duration类(时间间隔)

This commit is contained in:
2026-07-30 11:59:39 +08:00
parent 361bef782e
commit dbcca2c465
2 changed files with 33 additions and 1 deletions
@@ -1,5 +1,6 @@
package com.inmind.jdk8_date05;
import java.time.Duration;
import java.time.Instant;
/**
@@ -29,9 +30,14 @@ public class InstantDemo12 {
// Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
Instant start = Instant.now();
for (int i = 0; i < 500; i++) {
System.out.println("---------");
}
//代码的执行(50行代码的功能)
Instant end = Instant.now();
//todo 之后我们会用间隔来计算时间差!!!
//我们会用间隔来计算时间差!!!
Duration duration = Duration.between(start, end);
System.out.println(duration.toNanos());
//传统的Date类,只能精确到毫秒,并且是可变对象,而Instant可以精确到纳秒,并且不可改变对象,推荐使用Instant,但一般LocalDateTime也够用了
}
}