Java时间 之 Instant
生活随笔
收集整理的這篇文章主要介紹了
Java时间 之 Instant
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Instant是一種表示秒和毫秒的類> Instant是Java8中新提供的時間類,出場次數較少,但是小身板里也有一些需要注意的東西
官方說明精撿
- 這個類在時間線上模擬一個瞬時點(相當于流動的河水中我們舀出來了一瓢,舀出來之后就是靜止的)
- 可用于記錄事件的時間戳
- 這個類不可變,并且線程安全
類里面主要有什么
/*** The number of seconds from the epoch of 1970-01-01T00:00:00Z.*/private final long seconds;/*** The number of nanoseconds, later along the time-line, from the seconds field.* This is always positive, and never exceeds 999,999,999.*/private final int nanos;//-----------------------------------------------------------------------/*** Obtains the current instant from the system clock.* <p>* This will query the {@link Clock#systemUTC() system UTC clock} to* obtain the current instant.* <p>* Using this method will prevent the ability to use an alternate time-source for* testing because the clock is effectively hard-coded.** @return the current instant using the system clock, not null*/public static Instant now() {return Clock.systemUTC().instant();}實例
public static void main(String[] args) {Instant instant = Instant.now();System.out.println(instant.getEpochSecond());System.out.println(instant.getNano());}// OUT// 1564569225 =====> 北京時間:2019/7/31 18:33:45// 346000000 =====> 秒:0.346秒,可見這里也只是精確到了毫秒前面說到,調用的是System.currentTimeMillis()方法,這個方法只能返回毫秒,所以使用Instant.now()聲明的實例,肯定是只能表示到毫秒。
我的個人博客,有空來坐坐
總結
以上是生活随笔為你收集整理的Java时间 之 Instant的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抄底的代价
- 下一篇: python 模块相互import