日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java时间 之 Instant

發布時間:2023/12/16 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();}
  • **seconds**表示從1970-01-01T00:00:00Z到目前為止經過了多少秒
  • **nanos**表示從我們獲取的這個時間點的這一秒內,已經過了多少納秒
  • 獲取當前時間的**Instant**,最后調用的是System.currentTimeMillis()
  • 實例

    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秒,可見這里也只是精確到了毫秒
  • Instant.now()為什么只能精確到毫秒?
    前面說到,調用的是System.currentTimeMillis()方法,這個方法只能返回毫秒,所以使用Instant.now()聲明的實例,肯定是只能表示到毫秒。
  • 有沒有更精確的方法?
  • public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);return create(secs, nos);}

    我的個人博客,有空來坐坐

    總結

    以上是生活随笔為你收集整理的Java时间 之 Instant的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。