欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8 Instant時(shí)間戳使用小記

 更新時(shí)間:2021年01月04日 11:01:33   作者:strive_day  
這篇文章主要給大家介紹了關(guān)于Java8 Instant時(shí)間戳使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Java 8 Instant 時(shí)間戳

用于“時(shí)間戳”的運(yùn)算。它是以Unix元年(傳統(tǒng) 的設(shè)定為UTC時(shí)區(qū)1970年1月1日午夜時(shí)分)開始 所經(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(){
  // 通過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í)分秒的開始(或者日期與時(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 通過方法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 通過增加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. 通過Instant獲取當(dāng)前時(shí)間距離格林威治時(shí)間的值

通過 getEpochSecond()方法獲取距離格林威治時(shí)間的秒數(shù)

通過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í) 通過TimeUnit.HOURS.toMillis(1)將小時(shí)轉(zhuǎn)換為毫秒,然后通過plusMillis增加
  Instant instant3 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(1));
  //獲取時(shí)間戳 instant和instant3 相差天數(shù),返回long類型
  //如果小于1天,都算零天,大于等于1天,小于2天算一天
  System.out.println("相差天數(shù) = " + instant.until(instant3, ChronoUnit.DAYS)); //返回0
  //獲取時(shí)間戳 instant和instant3 相差的小時(shí)數(shù),返回long類型
  System.out.println("相差小時(shí) = " + instant.until(instant3, ChronoUnit.HOURS)); //返回1
  //獲取時(shí)間戳 instant和instant3 相差的毫秒數(shù),返回long類型
  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)文章

  • SpringBoot外部化配置示例解析

    SpringBoot外部化配置示例解析

    這篇文章主要介紹了SpringBoot外部化配置示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例

    Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例

    這篇文章主要介紹了Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能,結(jié)合實(shí)例形式分析了Java二維數(shù)組的遍歷、運(yùn)算、賦值等實(shí)現(xiàn)轉(zhuǎn)置的相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • JAVA中字符串如何與整型數(shù)字相加

    JAVA中字符串如何與整型數(shù)字相加

    這篇文章主要介紹了JAVA中字符串如何與整型數(shù)字相加,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Java數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列實(shí)例詳解

    Java數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列的相關(guān)資料,算是作為用java描述數(shù)據(jù)結(jié)構(gòu)的一個(gè)開始,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • Java線程間通信不同步問題原理與模擬實(shí)例

    Java線程間通信不同步問題原理與模擬實(shí)例

    這篇文章主要介紹了Java線程間通信不同步問題,結(jié)合實(shí)例形式分析了java線程間通信不同步問題的原理并模擬實(shí)現(xiàn)了線程間通信不同步情況下的異常輸出,需要的朋友可以參考下
    2019-10-10
  • SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway

    SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway

    這篇文章主要介紹了SpringCloud Gateway微服務(wù)網(wǎng)關(guān),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Java利用Netty時(shí)間輪實(shí)現(xiàn)延時(shí)任務(wù)

    Java利用Netty時(shí)間輪實(shí)現(xiàn)延時(shí)任務(wù)

    時(shí)間輪是一種可以執(zhí)行定時(shí)任務(wù)的數(shù)據(jù)結(jié)構(gòu)和算法。本文將為大家詳細(xì)講解一下Java如何利用Netty時(shí)間輪算法實(shí)現(xiàn)延時(shí)任務(wù),感興趣的小伙伴可以了解一下
    2022-08-08
  • 詳解Java synchronized關(guān)鍵字的用法

    詳解Java synchronized關(guān)鍵字的用法

    在多線程編程中常常使用鎖機(jī)制來確保同一時(shí)刻只有一個(gè)線程能夠修改共享內(nèi)存,在Java中一般是使用synchronized作為鎖機(jī)制,下面就讓我們來學(xué)習(xí)一下如何使用synchronized實(shí)現(xiàn)線程安全吧
    2023-08-08
  • 利用SpringDataJPA開啟審計(jì)功能,自動(dòng)保存操作人操作時(shí)間

    利用SpringDataJPA開啟審計(jì)功能,自動(dòng)保存操作人操作時(shí)間

    這篇文章主要介紹了利用SpringDataJPA開啟審計(jì)功能,自動(dòng)保存操作人操作時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java自定義注解接口實(shí)現(xiàn)方案

    java自定義注解接口實(shí)現(xiàn)方案

    java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運(yùn)行時(shí)進(jìn)行解析和使用,起到說明、配置的功能,本文將詳細(xì)介紹,此功能的實(shí)現(xiàn)方法
    2012-11-11

最新評(píng)論