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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【jdk1.8特性】之Instant

發布時間:2023/12/16 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【jdk1.8特性】之Instant 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

聲明本文只給出本人認為Instant中可能相對常用的方法的使用示例,更多用法細節可見API文檔或源碼。


  • 獲取(北京)瞬時時間點:

/*** 獲取當前(北京)的瞬時時間點*/ @Test public void test1() {// 獲取當前InstantInstant instant = Instant.now(Clock.systemDefaultZone());// 由于北京時間 比 UTC時間晚8小時,所以我們需要得出北京的瞬時時間,還需要加上8小時// 提示:雖然可以通過LocalDateTime、ZonedDateTime更輕易的獲得當前時間,但是本類// 既然是為了演示Instant,那么就只演示通過Instant如何獲得當前北京瞬時時間instant = instant.plus(8, ChronoUnit.HOURS);// 輸出形如: 2019-08-11T00:00:33.225Z// 注:不建議使用Instant查看當前時間點System.out.println(instant); }
  • Instant與時間偏移量的相互轉換:

/*** Instant 與 時間偏移量 的相互轉換** 注:從1970-01-01 00:00:00開始計算偏移*/ @Test public void test2() {System.out.println(" System.currentTimeMillis : " + System.currentTimeMillis());Instant instant = Instant.now(Clock.systemDefaultZone());// -> Instant => 時間偏移量(毫秒)long offSetMilliSecond = instant.toEpochMilli();System.out.println(" Instant => 時間偏移量(毫秒) : " + offSetMilliSecond);// -> Instant => 時間偏移量(秒)long offSetSecond = instant.getEpochSecond();System.out.println(" Instant => 時間偏移量(秒) : " + offSetSecond);// 獲取當前時間點(拋開整數秒不算)的納秒數// 如: 12345.12秒,拋開整數秒不算,則為0.12秒,那么instant.getNano()的結// 果為 0.12 * 1000_000_000 = 120_000_000int nano = instant.getNano();System.out.println(" 獲取當前時間點(拋開整數秒不算)的納秒數 : " + nano);// -> 時間偏移量(毫秒) => InstantInstant instant1 = Instant.ofEpochMilli(System.currentTimeMillis());// 將instant從 指向UTC時間 改為 指向北京時間instant1 = instant1.plus(8, ChronoUnit.HOURS);System.out.println(" 時間偏移量(毫秒) => Instant : " + instant1);// -> 時間偏移量(秒) => InstantInstant instant2 = Instant.ofEpochSecond(System.currentTimeMillis() / 1000);// 將instant從 指向UTC時間 改為 指向北京時間instant2 = instant2.plus(8, ChronoUnit.HOURS);System.out.println(" 時間偏移量(秒) => Instant : " + instant2); }
  • Instant的時間加、減:

/*** Instant的時間【加】、【減】** 注:下面的輸出結果形如:* 原(北京瞬時)instant -> 2019-08-11T01:18:46.261Z* 原(北京瞬時)instant + 1小時,結果是 -> 2019-08-11T02:18:46.261Z* 原(北京瞬時)instant - 2小時,結果是 -> 2019-08-10T23:18:46.261Z*/ @Test public void test3() {// 獲取當前瞬時時間,并轉換為北京的瞬時時間Instant instant = Instant.now(Clock.systemDefaultZone());instant = instant.plus(8, ChronoUnit.HOURS);System.out.println("原(北京瞬時)instant -> " + instant);// + 1 小時Instant plusRes = instant.plus(1, ChronoUnit.HOURS);System.out.println("原(北京瞬時)instant + 1小時,結果是 -> " + plusRes);// - 2 小時Instant minusRes = instant.minus(2, ChronoUnit.HOURS);System.out.println("原(北京瞬時)instant - 2小時,結果是 -> " + minusRes); }
  • 判斷兩個Instant之間誰早誰晚:

/*** 判斷兩個Instant之間誰早誰晚** 注:下面的輸出結果形如:* 瞬時時間點instantOne晚于instantTwo ? --- false* 瞬時時間點instantOne早于instantTwo ? --- true*/ @Test public void test4() {// Instant 一Instant instantOne = Instant.now(Clock.systemDefaultZone());// Instant 二Instant instantTwo = instantOne.plus(8, ChronoUnit.HOURS);boolean isAfterResult = instantOne.isAfter(instantTwo);System.out.println("瞬時時間點instantOne晚于instantTwo ? --- " + isAfterResult);boolean isBeforeResult = instantOne.isBefore(instantTwo);System.out.println("瞬時時間點instantOne早于instantTwo ? --- " + isBeforeResult); }

?

^_^?如有不當之處,歡迎指正

^_^?參考文檔
? ? ? ? ? ? ??
《jdk api 1.8_google.CHM》

^_^ 測試代碼托管鏈接
? ? ? ? ? ? ? ?
https://github.com/JustryDeng…Jdk8Feature

^_^?本文已經被收錄進《程序員成長筆記(五)》,筆者JustryDeng

總結

以上是生活随笔為你收集整理的【jdk1.8特性】之Instant的全部內容,希望文章能夠幫你解決所遇到的問題。

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