Instant类其实类似于之前学过的java.lang.System类,都是跟时间戳有关的。
// System.currentTimeMillis()返回一个long型的时间戳,单位ms。
为什么要用到时间戳:
//now():静态、实例化方法,创建一个Instant对象
// 获取本初子午线(UTC时区)对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2019-02-18T07:29:41.719Z 其时间与北京时间相差八个小时
//添加时间的偏移量
//偏移八个小时,对应北京时间
//因为新造了个对象,也体现了JDK8之后的时间api的不可变性
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
//返回的也是UTC区对应的时间戳,不是东八区
long milli = instant.toEpochMilli();
System.out.println(milli);
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
//注意毫秒参数后加L(long型)
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
System.out.println(instant1);