Java時間戳類Instant的使用詳解
前言
在JAVA8之前的版本,去獲取時間戳(毫秒級別)常用的辦法有兩種
// 方法一:構建日期Date類然后調用getTime方法 Date date = new Date(); System.out.println(date.getTime()); // 方法二:使用System類靜態(tài)方法獲取 System.out.println(System.currentTimeMillis());
由于Date類大部分方法已經廢棄,而且上面兩種方法的時間戳只能精確到毫秒級別,所以我們有必要了解下jdk1.8推出的Instant類,該類可以將時間戳精確到納秒級別。
Instant類
時間點
該類對象表示的是時間線上的一點,這個時間點存在標準的UTC時間,注意這個時間并不是指北京時間或東京時間而是指世界時間。
// 獲取當前時間 2022-09-26T03:12:58.517Z(比當地時間相差8個小時) System.out.println(Instant.now()); // 獲取系統(tǒng)默認時間戳 2022-09-26T11:12:58.517+08:00[Asia/Shanghai] System.out.println(Instant.now().atZone(ZoneId.systemDefault()));
在Instant時間線上存在三個重要的點位,最大點、最小點、原點也就是說小于1970-01-01的時間戳就為負數,超過1970-01-01的時間戳就為正數
// 時間線上最大點 +1000000000-12-31T23:59:59.999999999Z
System.out.println(Instant.MAX);
// 時間線上最小點 -1000000000-01-01T00:00:00Z
System.out.println(Instant.MIN);
// 時間線上原點 1970-01-01T00:00:00Z
System.out.println(Instant.EPOCH);
// 輸出結果為-8369623
System.out.println(Instant.parse("1969-09-26T03:06:17.323Z").getEpochSecond());
時間表示
在Instant中采用兩個字段表示時間戳
/** * The number of seconds from the epoch of 1970-01-01T00:00:00Z. * 該字段表示Instant時間距離原點1970-01-01T00:00:00Z的時間(單位秒) */ 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當前時間的納秒數這個值不會超過999,999,999,因為1秒=1000_000_000納秒 */ private final int nanos;
Instant實例化
普通實例化分為如下幾種
// 獲取當前時間
Instant instant1 = Instant.now();
// 字符串轉Instant
Instant instant2 = Instant.parse("2022-09-26T03:46:24.373Z");
// 構建秒級Instant對象,從時間1970-01-01T00:00:00Z開始計算(距離原點5000秒)
// 結果為:1970-01-01T01:23:20Z
Instant instant3 = Instant.ofEpochSecond(5000);
// 構建毫秒級Instant對象,同樣從時間1970-01-01T00:00:00Z開始計算(距離原點5000毫秒)
// 結果為:1970-01-01T00:00:05Z
Instant instant4 = Instant.ofEpochMilli(5000);
還有一種特殊的如下,可以構建納秒級的Instant對象
// 構建納秒級Instant對象,同樣從時間1970-01-01T00:00:00Z開始計算 // 參數:epochSecond(秒),nanoAdjustment(納秒) // 結果為:1970-01-01T00:00:05.000001111Z Instant instant5 = Instant.ofEpochSecond(5, 1111);
不過我們需要注意Instant.ofEpochSecond方法的源碼,如下
static final long NANOS_PER_SECOND = 1000_000_000L;
/**
* @param epochSecond 秒從1970-01-01T00:00:00Z開始計算
* @param nanoAdjustment 納秒
*/
public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
// Math.floorDiv是除法運算,返回小于或等于商的整數 Math.floorDiv(25, 3)=8
// Math.addExact加法運算,Math.addExact(1, 2)=3
long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
// Math.floorMod是模運算,Math.floorMod(9, 20)=9
int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
return create(secs, nos);
}
Instant獲取參數
Instant instant = Instant.now();
// 時區(qū)相差8小時 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時間點比較
由于時間點位于時間線上,所以可以直接進行對比。
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時間點運算
Instant instant1 = Instant.parse("2022-09-26T07:04:19.110Z");
// 在instant1的基礎上增加2秒,值為:2022-09-26T07:04:21.110Z
System.out.println(instant1.plusSeconds(2));
// 在instant1的基礎上增加1毫秒,值為:2022-09-26T07:04:19.111Z
System.out.println(instant1.plusMillis(1));
// 在instant1的基礎上增加1001納秒,值為:2022-09-26T07:04:19.110001001Z
System.out.println(instant1.plusNanos(1001));
// 在instant1的基礎上增加1秒,值為:2022-09-26T07:04:20.110Z
// 該值取決于后面指定的單位,可以從ChronoUnit枚舉類獲取
System.out.println(instant1.plus(1, ChronoUnit.SECONDS));
// 在instant1的基礎上減去1秒,值為:2022-09-26T07:04:18.110Z
// plus是增加,minus是減少,邏輯類似可以參考上面plus相關A
System.out.println(instant1.minusSeconds(1));
Instant時間點計算時需要注意,無論是調用plus或者minus相關API都會重新創(chuàng)建新對象。
到此這篇關于Java時間戳類Instant的使用詳解的文章就介紹到這了,更多相關Java時間戳類Instant內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Kotlin基礎教程之dataclass,objectclass,use函數,類擴展,socket
這篇文章主要介紹了Kotlin基礎教程之dataclass,objectclass,use函數,類擴展,socket的相關資料,需要的朋友可以參考下2017-05-05
Spring?Boot?配置?Hikari?數據庫連接池的操作代碼
數據庫連接池是一個提高程序與數據庫的連接的優(yōu)化,連接池它主要作用是提高性能、節(jié)省資源、控制連接數、連接管理等操作,這篇文章主要介紹了SpringBoot配置Hikari數據庫連接池,需要的朋友可以參考下2023-09-09
Java中JWT(JSON?Web?Token)的運用具體案例
這篇文章主要介紹了Java中JWT(JSON?Web?Token)的運用具體案例,JWT(JSON?Web?Token)是一種開放標準,用于在網絡應用環(huán)境中安全地傳遞信息,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-11-11

