Java8 Instant時(shí)間戳使用小記
Java 8 Instant 時(shí)間戳
用于“時(shí)間戳”的運(yùn)算。它是以Unix元年(傳統(tǒng) 的設(shè)定為UTC時(shí)區(qū)1970年1月1日午夜時(shí)分)開(kāi)始 所經(jīng)歷的描述進(jìn)行運(yùn)算
1. 創(chuàng)建Instant實(shí)例,獲取系統(tǒng)的當(dāng)前時(shí)間now
/** * Java 8 Instant時(shí)間戳學(xué)習(xí) */ @Test public void testInstant(){ // 通過(guò)Instant創(chuàng)建Instant實(shí)例 返回:return Clock.systemUTC().instant(); Instant now = Instant.now(); //控制臺(tái)輸出:now = 2020-12-29T06:32:49.480Z (以ISO-8601格式輸出) System.out.println("now = " + now); }
注意:這里額控制臺(tái)輸出:now = 2020-12-29T06:32:49.480Z。
Intance的now方法:
public static Instant now() { return Clock.systemUTC().instant(); }
這是輸出的世界標(biāo)準(zhǔn)時(shí)間,其中T表示時(shí)分秒的開(kāi)始(或者日期與時(shí)間的間隔),Z表示這是一個(gè)世界標(biāo)準(zhǔn)時(shí)間。
Instant 是時(shí)間戳,是指世界標(biāo)準(zhǔn)時(shí)格林威治時(shí)間1970年01月01日00時(shí)00分00秒(北京時(shí)間1970年01月01日08時(shí)00分00秒)起至現(xiàn)在的總秒數(shù),Instant本身實(shí)際上是指明時(shí)區(qū)了,是0時(shí)區(qū)(也就是比北京時(shí)間少8小時(shí))。
2. 獲取當(dāng)前時(shí)區(qū)的時(shí)間(本地時(shí)間)
2.1 通過(guò)方法Instant.now().atZone(ZoneId.systemDefault())獲取當(dāng)前地區(qū)的時(shí)間
ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.systemDefault()); System.out.println(zonedDateTime);
輸出結(jié)果
2020-12-31T17:31:14.953+08:00[Asia/Shanghai]
2.2 通過(guò)增加8小時(shí),轉(zhuǎn)化為北京時(shí)間
方法名稱 | 描述 |
---|---|
plusMillis() | 增加時(shí)間戳?xí)r間,以毫秒為單位 |
minusNanos() | 增加時(shí)間戳?xí)r間,以納秒為單位 |
minusSeconds() | 增加時(shí)間戳?xí)r間,以秒為單位 |
TimeUnit.HOURS.toMillis() | 將小時(shí)轉(zhuǎn)化為毫秒數(shù) |
//增加8個(gè)小時(shí),使Instant.now()返回時(shí)間為北京時(shí)間 Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); System.out.println("now2 = " + now2);
輸出結(jié)果:now2 = 2020-12-29T14:35:32.631Z
轉(zhuǎn)換為符合當(dāng)前的北京時(shí)間。
3. 通過(guò)Instant獲取當(dāng)前時(shí)間距離格林威治時(shí)間的值
通過(guò) getEpochSecond()方法獲取距離格林威治時(shí)間的秒數(shù)
通過(guò)toEpochMilli()方法獲取距離格林威治時(shí)間的毫秒數(shù)
//增加8個(gè)小時(shí),使Instant.now()返回時(shí)間為北京時(shí)間 Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); //獲取格林威治時(shí)間1970年01月01日00時(shí)00分00秒(北京時(shí)間1970年01月01日08時(shí)00分00秒)距離當(dāng)前時(shí)間的秒/毫秒值 System.out.println("距離1970年01月01日00時(shí)00分00秒 : "+now2.getEpochSecond() + "秒"); System.out.println("距離1970年01月01日00時(shí)00分00秒 : "+now2.toEpochMilli() + "毫秒");
輸出結(jié)果:
距離1970年01月01日00時(shí)00分00秒 : 1609435201秒
距離1970年01月01日00時(shí)00分00秒 : 1609435201645毫秒
4. Instant的from、parse方法
4.1 java.time.Instant.from(TemporalAccessor temporal)源碼:
public static Instant from(TemporalAccessor temporal) { if (temporal instanceof Instant) { return (Instant) temporal; } Objects.requireNonNull(temporal, "temporal"); try { long instantSecs = temporal.getLong(INSTANT_SECONDS); int nanoOfSecond = temporal.get(NANO_OF_SECOND); return Instant.ofEpochSecond(instantSecs, nanoOfSecond); } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName(), ex); } }
參數(shù):temporal 是要轉(zhuǎn)換的時(shí)間對(duì)象,返回的是一個(gè)轉(zhuǎn)換為Instant的瞬間值
如果轉(zhuǎn)換為Instant的時(shí)候失敗,會(huì)拋出異常``DateTimeException`
4.2 parse方法源碼
public static Instant parse(final CharSequence text) { return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from); }
創(chuàng)建自定義的時(shí)間戳
//創(chuàng)建自定義的時(shí)間戳 System.out.println(Instant.parse("2020-12-29T14:35:32.631Z"));
輸出結(jié)果
2020-12-29T14:35:32.631Z
5. Instant的其它常用函數(shù)
//獲取當(dāng)前時(shí)間戳 Instant instant = Instant.now(); //獲得當(dāng)前時(shí)間戳并且增加66毫秒 Instant instant1 = Instant.now().plusMillis(66); //獲得當(dāng)前時(shí)間戳并且減少66毫秒 Instant instant2 = Instant.now().minusMillis(66); //判斷時(shí)間戳 instant 是否在 instant1 之后,返回boolean System.out.println(instant.isAfter(instant1)); //返回false //判斷時(shí)間戳 instant 是否在 instant1 之前,返回boolean System.out.println(instant.isBefore(instant1)); //返回true //判斷兩個(gè)時(shí)間戳是否相等, 返回boolean值 System.out.println(instant.equals(instant1)); //返回false //獲得當(dāng)前時(shí)間戳并增加1小時(shí) 通過(guò)TimeUnit.HOURS.toMillis(1)將小時(shí)轉(zhuǎn)換為毫秒,然后通過(guò)plusMillis增加 Instant instant3 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(1)); //獲取時(shí)間戳 instant和instant3 相差天數(shù),返回long類(lèi)型 //如果小于1天,都算零天,大于等于1天,小于2天算一天 System.out.println("相差天數(shù) = " + instant.until(instant3, ChronoUnit.DAYS)); //返回0 //獲取時(shí)間戳 instant和instant3 相差的小時(shí)數(shù),返回long類(lèi)型 System.out.println("相差小時(shí) = " + instant.until(instant3, ChronoUnit.HOURS)); //返回1 //獲取時(shí)間戳 instant和instant3 相差的毫秒數(shù),返回long類(lèi)型 System.out.println("相差毫秒數(shù) = " + instant.until(instant3, ChronoUnit.MILLIS)); //返回3600000
輸出結(jié)果:
false
true
false
相差天數(shù) = 0
相差小時(shí) = 1
相差毫秒數(shù) = 3600000
6. 將獲取的時(shí)間戳轉(zhuǎn)化為L(zhǎng)ocalDate
Instant now = Instant.now(); //UTC ZonedDateTime atZone = now.atZone(ZoneOffset.UTC); //LocalDateTime atZone.toLocalDateTime(); LocalDateTime.from(atZone); //LocalDate atZone.toLocalDate(); LocalDate date = LocalDate.from(atZone); //LocalDateTime atZone.toLocalDateTime(); LocalDateTime.from(date);
總結(jié)
到此這篇關(guān)于Java8 Instant時(shí)間戳使用小記的文章就介紹到這了,更多相關(guān)Java8 Instant時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jmeter連接Mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Jmeter連接Mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08使用springBoot中的info等級(jí)通過(guò)druid打印sql
這篇文章主要介紹了使用springBoot中的info等級(jí)通過(guò)druid打印sql,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java中利用反射調(diào)用另一類(lèi)的private方法的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java中利用反射調(diào)用另一類(lèi)的private方法的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06Spring集成Web環(huán)境與SpringMVC組件的擴(kuò)展使用詳解
這篇文章主要介紹了Spring集成Web環(huán)境與SpringMVC組件,它是一個(gè)MVC架構(gòu),用來(lái)簡(jiǎn)化基于MVC架構(gòu)的Web應(yīng)用開(kāi)發(fā)。SpringMVC最重要的就是五大組件2022-08-08Nacos啟動(dòng)出現(xiàn)failed to req API:/nacos/v1/ns/insta
這篇文章主要介紹了Nacos啟動(dòng)出現(xiàn)failed to req API:/nacos/v1/ns/instance after all servers問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來(lái)優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄功能(多 Realm 認(rèn)證)
這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄(多 Realm 認(rèn)證),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02