s-day01--Instant类 (代替Date)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package com.inmind.jdk8_time04;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* 目标:掌握Instant的使用。
|
||||
*/
|
||||
public class InstantDemo11 {
|
||||
public static void main(String[] args) {
|
||||
//1.创建时间戳对象
|
||||
Instant now = Instant.now();
|
||||
|
||||
//2.获取总秒数(计算机基准时间开始算的)
|
||||
long epochSecond = now.getEpochSecond();
|
||||
System.out.println(epochSecond);
|
||||
//3.不够1秒的纳秒值
|
||||
System.out.println(now.getNano());
|
||||
//4.打印对象:总秒数+纳秒数
|
||||
System.out.println(now);//重写toString
|
||||
//5.对时间点的加减
|
||||
Instant instant1 = now.plusSeconds(60);
|
||||
Instant instant2 = now.minusSeconds(60);
|
||||
System.out.println(instant1);
|
||||
System.out.println(instant2);
|
||||
//6.Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
|
||||
Instant start = Instant.now();
|
||||
//业务代码的执行....
|
||||
for (int i = 0; i < 100; i++) {
|
||||
System.out.println(1);
|
||||
}
|
||||
Instant end = Instant.now();
|
||||
//todo 之后使用间隔来计算时间的差
|
||||
|
||||
//传统的Date类,只能精确到毫秒,并且是可变对象,而Instant可以精确到纳秒,并且不可改变对象,推荐使用Instant,但一般LocalDateTime也够用了
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user