Java時(shí)間戳類(lèi)Instant的使用詳解
前言
在JAVA8之前的版本,去獲取時(shí)間戳(毫秒級(jí)別)常用的辦法有兩種
// 方法一:構(gòu)建日期Date類(lèi)然后調(diào)用getTime方法 Date date = new Date(); System.out.println(date.getTime()); // 方法二:使用System類(lèi)靜態(tài)方法獲取 System.out.println(System.currentTimeMillis());
由于Date類(lèi)大部分方法已經(jīng)廢棄,而且上面兩種方法的時(shí)間戳只能精確到毫秒級(jí)別,所以我們有必要了解下jdk1.8推出的Instant類(lèi),該類(lèi)可以將時(shí)間戳精確到納秒級(jí)別。
Instant類(lèi)
時(shí)間點(diǎn)
該類(lèi)對(duì)象表示的是時(shí)間線(xiàn)上的一點(diǎn),這個(gè)時(shí)間點(diǎn)存在標(biāo)準(zhǔn)的UTC時(shí)間,注意這個(gè)時(shí)間并不是指北京時(shí)間或東京時(shí)間而是指世界時(shí)間。
// 獲取當(dāng)前時(shí)間 2022-09-26T03:12:58.517Z(比當(dāng)?shù)貢r(shí)間相差8個(gè)小時(shí)) System.out.println(Instant.now()); // 獲取系統(tǒng)默認(rèn)時(shí)間戳 2022-09-26T11:12:58.517+08:00[Asia/Shanghai] System.out.println(Instant.now().atZone(ZoneId.systemDefault()));
在Instant時(shí)間線(xiàn)上存在三個(gè)重要的點(diǎn)位,最大點(diǎn)、最小點(diǎn)、原點(diǎn)也就是說(shuō)小于1970-01-01的時(shí)間戳就為負(fù)數(shù),超過(guò)1970-01-01的時(shí)間戳就為正數(shù)
// 時(shí)間線(xiàn)上最大點(diǎn) +1000000000-12-31T23:59:59.999999999Z System.out.println(Instant.MAX); // 時(shí)間線(xiàn)上最小點(diǎn) -1000000000-01-01T00:00:00Z System.out.println(Instant.MIN); // 時(shí)間線(xiàn)上原點(diǎn) 1970-01-01T00:00:00Z System.out.println(Instant.EPOCH); // 輸出結(jié)果為-8369623 System.out.println(Instant.parse("1969-09-26T03:06:17.323Z").getEpochSecond());
時(shí)間表示
在Instant中采用兩個(gè)字段表示時(shí)間戳
/** * The number of seconds from the epoch of 1970-01-01T00:00:00Z. * 該字段表示Instant時(shí)間距離原點(diǎn)1970-01-01T00:00:00Z的時(shí)間(單位秒) */ 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. * 該字段表示Instant當(dāng)前時(shí)間的納秒數(shù)這個(gè)值不會(huì)超過(guò)999,999,999,因?yàn)?秒=1000_000_000納秒 */ private final int nanos;
Instant實(shí)例化
普通實(shí)例化分為如下幾種
// 獲取當(dāng)前時(shí)間 Instant instant1 = Instant.now(); // 字符串轉(zhuǎn)Instant Instant instant2 = Instant.parse("2022-09-26T03:46:24.373Z"); // 構(gòu)建秒級(jí)Instant對(duì)象,從時(shí)間1970-01-01T00:00:00Z開(kāi)始計(jì)算(距離原點(diǎn)5000秒) // 結(jié)果為:1970-01-01T01:23:20Z Instant instant3 = Instant.ofEpochSecond(5000); // 構(gòu)建毫秒級(jí)Instant對(duì)象,同樣從時(shí)間1970-01-01T00:00:00Z開(kāi)始計(jì)算(距離原點(diǎn)5000毫秒) // 結(jié)果為:1970-01-01T00:00:05Z Instant instant4 = Instant.ofEpochMilli(5000);
還有一種特殊的如下,可以構(gòu)建納秒級(jí)的Instant對(duì)象
// 構(gòu)建納秒級(jí)Instant對(duì)象,同樣從時(shí)間1970-01-01T00:00:00Z開(kāi)始計(jì)算 // 參數(shù):epochSecond(秒),nanoAdjustment(納秒) // 結(jié)果為:1970-01-01T00:00:05.000001111Z Instant instant5 = Instant.ofEpochSecond(5, 1111);
不過(guò)我們需要注意Instant.ofEpochSecond方法的源碼,如下
static final long NANOS_PER_SECOND = 1000_000_000L; /** * @param epochSecond 秒從1970-01-01T00:00:00Z開(kāi)始計(jì)算 * @param nanoAdjustment 納秒 */ public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) { // Math.floorDiv是除法運(yùn)算,返回小于或等于商的整數(shù) Math.floorDiv(25, 3)=8 // Math.addExact加法運(yùn)算,Math.addExact(1, 2)=3 long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND)); // Math.floorMod是模運(yùn)算,Math.floorMod(9, 20)=9 int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND); return create(secs, nos); }
Instant獲取參數(shù)
Instant instant = Instant.now(); // 時(shí)區(qū)相差8小時(shí) 2022-09-26T07:04:19.110Z System.out.println(instant); System.out.println("秒:"+instant.getEpochSecond()); System.out.println("毫秒:"+instant.toEpochMilli()); // 1毫秒 = 1000 000 納秒 System.out.println("納秒:"+instant.getNano());
Instant時(shí)間點(diǎn)比較
由于時(shí)間點(diǎn)位于時(shí)間線(xiàn)上,所以可以直接進(jìn)行對(duì)比。
Instant instant1 = Instant.parse("2022-09-26T07:04:19.110Z"); Instant instant2 = Instant.parse("2022-09-26T07:04:19.110Z"); Instant instant3 = Instant.parse("2022-08-26T07:04:19.110Z"); // 相等為0 System.out.println(instant1.compareTo(instant2)); // instant1大于instant3 為1 System.out.println(instant1.compareTo(instant3)); // instant1小于instant3 為-1 System.out.println(instant3.compareTo(instant1)); // true System.out.println(instant1.isAfter(instant3)); // false System.out.println(instant1.isBefore(instant3));
Instant時(shí)間點(diǎn)運(yùn)算
Instant instant1 = Instant.parse("2022-09-26T07:04:19.110Z"); // 在instant1的基礎(chǔ)上增加2秒,值為:2022-09-26T07:04:21.110Z System.out.println(instant1.plusSeconds(2)); // 在instant1的基礎(chǔ)上增加1毫秒,值為:2022-09-26T07:04:19.111Z System.out.println(instant1.plusMillis(1)); // 在instant1的基礎(chǔ)上增加1001納秒,值為:2022-09-26T07:04:19.110001001Z System.out.println(instant1.plusNanos(1001)); // 在instant1的基礎(chǔ)上增加1秒,值為:2022-09-26T07:04:20.110Z // 該值取決于后面指定的單位,可以從ChronoUnit枚舉類(lèi)獲取 System.out.println(instant1.plus(1, ChronoUnit.SECONDS)); // 在instant1的基礎(chǔ)上減去1秒,值為:2022-09-26T07:04:18.110Z // plus是增加,minus是減少,邏輯類(lèi)似可以參考上面plus相關(guān)A System.out.println(instant1.minusSeconds(1));
Instant時(shí)間點(diǎn)計(jì)算時(shí)需要注意,無(wú)論是調(diào)用plus或者minus相關(guān)API都會(huì)重新創(chuàng)建新對(duì)象。
到此這篇關(guān)于Java時(shí)間戳類(lèi)Instant的使用詳解的文章就介紹到這了,更多相關(guān)Java時(shí)間戳類(lèi)Instant內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的Spring框架中實(shí)現(xiàn)發(fā)送郵件功能的核心代碼示例
這篇文章主要介紹了Java的Spring框架中實(shí)現(xiàn)發(fā)送郵件功能的核心代碼示例,包括發(fā)送帶附件的郵件功能的實(shí)現(xiàn),需要的朋友可以參考下2016-03-03AQS同步組件CyclicBarrier循環(huán)屏障用例剖析
這篇文章主要為大家介紹了AQS同步組件CyclicBarrier循環(huán)屏障用例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Java、Javascript、Javaweb三者的區(qū)別及說(shuō)明
這篇文章主要介紹了Java、Javascript、Javaweb三者的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Java打包之后讀取Resources下的文件失效原因及解決方法
這篇文章主要給大家介紹了Java打包之后讀取Resources下的文件失效的問(wèn)題分析和解決方法,文中通過(guò)代碼示例和圖文結(jié)合給大家講解非常詳細(xì),需要的朋友可以參考下2023-12-12Java基礎(chǔ)之ArrayList的擴(kuò)容機(jī)制
這篇文章主要介紹了Java基礎(chǔ)之ArrayList的擴(kuò)容機(jī)制,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05利用SpringMVC接收復(fù)雜對(duì)象和多個(gè)文件(前端使用JQuery)
這篇文章主要介紹了利用SpringMVC接收復(fù)雜對(duì)象和多個(gè)文件(前端使用JQuery),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10利用JavaFX工具構(gòu)建Reactive系統(tǒng)
這篇文章主要介紹了使用JavaFX構(gòu)建Reactive系統(tǒng),利用JavaFX工具集中的新的超棒特性來(lái)構(gòu)建響應(yīng)式的快速應(yīng)用程序,感興趣的小伙伴們可以參考一下2016-02-02SpringCloud集成Hystrix熔斷過(guò)程分步分解
通過(guò)hystrix可以解決雪崩效應(yīng)問(wèn)題,它提供了資源隔離、降級(jí)機(jī)制、融斷、緩存等功能。接下來(lái)通過(guò)本文給大家分享SpringCloud集成Hystrix熔斷,感興趣的朋友一起看看吧2022-09-09