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

Java時間戳類Instant的使用詳解

 更新時間:2022年09月29日 10:32:27   作者:Java面試365  
這篇文章主要為大家詳細介紹了Java中時間戳類Instant的使用方法,文中的示例代碼講解詳細,對我們學習Java有一定幫助,需要的可以參考一下

前言

在JAVA8之前的版本,去獲取時間戳(毫秒級別)常用的辦法有兩種

// 方法一:構(gòu)建日期Date類然后調(diào)用getTime方法
Date date = new Date();
System.out.println(date.getTime());

// 方法二:使用System類靜態(tài)方法獲取
System.out.println(System.currentTimeMillis());

由于Date類大部分方法已經(jīng)廢棄,而且上面兩種方法的時間戳只能精確到毫秒級別,所以我們有必要了解下jdk1.8推出的Instant類,該類可以將時間戳精確到納秒級別。

Instant類

時間點

該類對象表示的是時間線上的一點,這個時間點存在標準的UTC時間,注意這個時間并不是指北京時間或東京時間而是指世界時間。

// 獲取當前時間 2022-09-26T03:12:58.517Z(比當?shù)貢r間相差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的時間戳就為負數(shù),超過1970-01-01的時間戳就為正數(shù)

// 時間線上最大點  +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);

// 輸出結(jié)果為-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當前時間的納秒數(shù)這個值不會超過999,999,999,因為1秒=1000_000_000納秒
 */
private final int nanos;

Instant實例化

普通實例化分為如下幾種

// 獲取當前時間
Instant instant1 = Instant.now();

// 字符串轉(zhuǎn)Instant
Instant instant2 = Instant.parse("2022-09-26T03:46:24.373Z");

// 構(gòu)建秒級Instant對象,從時間1970-01-01T00:00:00Z開始計算(距離原點5000秒)
// 結(jié)果為:1970-01-01T01:23:20Z
Instant instant3 = Instant.ofEpochSecond(5000);

// 構(gòu)建毫秒級Instant對象,同樣從時間1970-01-01T00:00:00Z開始計算(距離原點5000毫秒)
// 結(jié)果為:1970-01-01T00:00:05Z
Instant instant4 = Instant.ofEpochMilli(5000);

還有一種特殊的如下,可以構(gòu)建納秒級的Instant對象

// 構(gòu)建納秒級Instant對象,同樣從時間1970-01-01T00:00:00Z開始計算
// 參數(shù):epochSecond(秒),nanoAdjustment(納秒)
// 結(jié)果為: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是除法運算,返回小于或等于商的整數(shù) 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獲取參數(shù)

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的基礎(chǔ)上增加2秒,值為:2022-09-26T07:04:21.110Z
System.out.println(instant1.plusSeconds(2));

// 在instant1的基礎(chǔ)上增加1毫秒,值為:2022-09-26T07:04:19.111Z
System.out.println(instant1.plusMillis(1));

// 在instant1的基礎(chǔ)上增加1001納秒,值為:2022-09-26T07:04:19.110001001Z
System.out.println(instant1.plusNanos(1001));

// 在instant1的基礎(chǔ)上增加1秒,值為:2022-09-26T07:04:20.110Z
// 該值取決于后面指定的單位,可以從ChronoUnit枚舉類獲取
System.out.println(instant1.plus(1, ChronoUnit.SECONDS));

// 在instant1的基礎(chǔ)上減去1秒,值為:2022-09-26T07:04:18.110Z
// plus是增加,minus是減少,邏輯類似可以參考上面plus相關(guān)A
System.out.println(instant1.minusSeconds(1));

Instant時間點計算時需要注意,無論是調(diào)用plus或者minus相關(guān)API都會重新創(chuàng)建新對象。

到此這篇關(guān)于Java時間戳類Instant的使用詳解的文章就介紹到這了,更多相關(guān)Java時間戳類Instant內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論