Java8 Instant時間戳使用小記
Java 8 Instant 時間戳
用于“時間戳”的運算。它是以Unix元年(傳統(tǒng) 的設(shè)定為UTC時區(qū)1970年1月1日午夜時分)開始 所經(jīng)歷的描述進(jìn)行運算
1. 創(chuàng)建Instant實例,獲取系統(tǒng)的當(dāng)前時間now
/**
* Java 8 Instant時間戳學(xué)習(xí)
*/
@Test
public void testInstant(){
// 通過Instant創(chuàng)建Instant實例 返回:return Clock.systemUTC().instant();
Instant now = Instant.now();
//控制臺輸出:now = 2020-12-29T06:32:49.480Z (以ISO-8601格式輸出)
System.out.println("now = " + now);
}
注意:這里額控制臺輸出:now = 2020-12-29T06:32:49.480Z。
Intance的now方法:
public static Instant now() {
return Clock.systemUTC().instant();
}
這是輸出的世界標(biāo)準(zhǔn)時間,其中T表示時分秒的開始(或者日期與時間的間隔),Z表示這是一個世界標(biāo)準(zhǔn)時間。
Instant 是時間戳,是指世界標(biāo)準(zhǔn)時格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現(xiàn)在的總秒數(shù),Instant本身實際上是指明時區(qū)了,是0時區(qū)(也就是比北京時間少8小時)。
2. 獲取當(dāng)前時區(qū)的時間(本地時間)
2.1 通過方法Instant.now().atZone(ZoneId.systemDefault())獲取當(dāng)前地區(qū)的時間
ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.systemDefault()); System.out.println(zonedDateTime);
輸出結(jié)果
2020-12-31T17:31:14.953+08:00[Asia/Shanghai]
2.2 通過增加8小時,轉(zhuǎn)化為北京時間
| 方法名稱 | 描述 |
|---|---|
| plusMillis() | 增加時間戳?xí)r間,以毫秒為單位 |
| minusNanos() | 增加時間戳?xí)r間,以納秒為單位 |
| minusSeconds() | 增加時間戳?xí)r間,以秒為單位 |
| TimeUnit.HOURS.toMillis() | 將小時轉(zhuǎn)化為毫秒數(shù) |
//增加8個小時,使Instant.now()返回時間為北京時間
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)前的北京時間。
3. 通過Instant獲取當(dāng)前時間距離格林威治時間的值
通過 getEpochSecond()方法獲取距離格林威治時間的秒數(shù)
通過toEpochMilli()方法獲取距離格林威治時間的毫秒數(shù)
//增加8個小時,使Instant.now()返回時間為北京時間
Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
//獲取格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)距離當(dāng)前時間的秒/毫秒值
System.out.println("距離1970年01月01日00時00分00秒 : "+now2.getEpochSecond() + "秒");
System.out.println("距離1970年01月01日00時00分00秒 : "+now2.toEpochMilli() + "毫秒");
輸出結(jié)果:
距離1970年01月01日00時00分00秒 : 1609435201秒
距離1970年01月01日00時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)換的時間對象,返回的是一個轉(zhuǎn)換為Instant的瞬間值
如果轉(zhuǎn)換為Instant的時候失敗,會拋出異常``DateTimeException`
4.2 parse方法源碼
public static Instant parse(final CharSequence text) {
return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
}
創(chuàng)建自定義的時間戳
//創(chuàng)建自定義的時間戳
System.out.println(Instant.parse("2020-12-29T14:35:32.631Z"));
輸出結(jié)果
2020-12-29T14:35:32.631Z
5. Instant的其它常用函數(shù)
//獲取當(dāng)前時間戳
Instant instant = Instant.now();
//獲得當(dāng)前時間戳并且增加66毫秒
Instant instant1 = Instant.now().plusMillis(66);
//獲得當(dāng)前時間戳并且減少66毫秒
Instant instant2 = Instant.now().minusMillis(66);
//判斷時間戳 instant 是否在 instant1 之后,返回boolean
System.out.println(instant.isAfter(instant1)); //返回false
//判斷時間戳 instant 是否在 instant1 之前,返回boolean
System.out.println(instant.isBefore(instant1)); //返回true
//判斷兩個時間戳是否相等, 返回boolean值
System.out.println(instant.equals(instant1)); //返回false
//獲得當(dāng)前時間戳并增加1小時 通過TimeUnit.HOURS.toMillis(1)將小時轉(zhuǎn)換為毫秒,然后通過plusMillis增加
Instant instant3 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(1));
//獲取時間戳 instant和instant3 相差天數(shù),返回long類型
//如果小于1天,都算零天,大于等于1天,小于2天算一天
System.out.println("相差天數(shù) = " + instant.until(instant3, ChronoUnit.DAYS)); //返回0
//獲取時間戳 instant和instant3 相差的小時數(shù),返回long類型
System.out.println("相差小時 = " + instant.until(instant3, ChronoUnit.HOURS)); //返回1
//獲取時間戳 instant和instant3 相差的毫秒數(shù),返回long類型
System.out.println("相差毫秒數(shù) = " + instant.until(instant3, ChronoUnit.MILLIS)); //返回3600000
輸出結(jié)果:
false
true
false
相差天數(shù) = 0
相差小時 = 1
相差毫秒數(shù) = 3600000
6. 將獲取的時間戳轉(zhuǎn)化為LocalDate
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時間戳使用小記的文章就介紹到這了,更多相關(guān)Java8 Instant時間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java編程實現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例
這篇文章主要介紹了Java編程實現(xiàn)的二維數(shù)組轉(zhuǎn)置功能,結(jié)合實例形式分析了Java二維數(shù)組的遍歷、運算、賦值等實現(xiàn)轉(zhuǎn)置的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Java數(shù)據(jù)結(jié)構(gòu)之棧與隊列實例詳解
這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之棧與隊列的相關(guān)資料,算是作為用java描述數(shù)據(jù)結(jié)構(gòu)的一個開始,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11
SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway
這篇文章主要介紹了SpringCloud Gateway微服務(wù)網(wǎng)關(guān),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Java利用Netty時間輪實現(xiàn)延時任務(wù)
時間輪是一種可以執(zhí)行定時任務(wù)的數(shù)據(jù)結(jié)構(gòu)和算法。本文將為大家詳細(xì)講解一下Java如何利用Netty時間輪算法實現(xiàn)延時任務(wù),感興趣的小伙伴可以了解一下2022-08-08
詳解Java synchronized關(guān)鍵字的用法
在多線程編程中常常使用鎖機(jī)制來確保同一時刻只有一個線程能夠修改共享內(nèi)存,在Java中一般是使用synchronized作為鎖機(jī)制,下面就讓我們來學(xué)習(xí)一下如何使用synchronized實現(xiàn)線程安全吧2023-08-08
利用SpringDataJPA開啟審計功能,自動保存操作人操作時間
這篇文章主要介紹了利用SpringDataJPA開啟審計功能,自動保存操作人操作時間,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

