欧美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的基礎上增加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相關(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)文章

  • java設計模式理解依賴于抽象不依賴具體的分析

    java設計模式理解依賴于抽象不依賴具體的分析

    這篇文章主要為大家介紹了java設計模式的規(guī)則,理解依賴于抽象不依賴具體的示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • 解決IDEA新建xml文件顯示為普通的text文本問題

    解決IDEA新建xml文件顯示為普通的text文本問題

    IntelliJ IDEA新建XML文件時顯示為普通文本,可以通過以下步驟解決:1.檢查項目文件過濾器,確保沒有隱藏XML文件類型;2.在XML文件中添加或修改文件類型關(guān)聯(lián);3.如果問題依然存在,檢查并刪除自定義的文件類型過濾器
    2024-11-11
  • Kotlin基礎教程之dataclass,objectclass,use函數(shù),類擴展,socket

    Kotlin基礎教程之dataclass,objectclass,use函數(shù),類擴展,socket

    這篇文章主要介紹了Kotlin基礎教程之dataclass,objectclass,use函數(shù),類擴展,socket的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • java使用任務架構(gòu)執(zhí)行任務調(diào)度示例

    java使用任務架構(gòu)執(zhí)行任務調(diào)度示例

    在Java 5.0之前啟動一個任務是通過調(diào)用Thread類的start()方法來實現(xiàn)的,5.0里提供了一個新的任務執(zhí)行架構(gòu)使你可以輕松地調(diào)度和控制任務的執(zhí)行,并且可以建立一個類似數(shù)據(jù)庫連接池的線程池來執(zhí)行任務,下面看一個示例
    2014-01-01
  • Java實現(xiàn)LeetCode(報數(shù))

    Java實現(xiàn)LeetCode(報數(shù))

    這篇文章主要介紹了Java實現(xiàn)LeetCode(報數(shù)),本文通過使用java實現(xiàn)leetcode的報數(shù)題目和實現(xiàn)思路分析,需要的朋友可以參考下
    2021-06-06
  • Java如何在不存在文件夾的目錄下創(chuàng)建文件

    Java如何在不存在文件夾的目錄下創(chuàng)建文件

    這篇文章主要介紹了Java如何在不存在文件夾的目錄下創(chuàng)建文件,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-08-08
  • Spring?Boot?配置?Hikari?數(shù)據(jù)庫連接池的操作代碼

    Spring?Boot?配置?Hikari?數(shù)據(jù)庫連接池的操作代碼

    數(shù)據(jù)庫連接池是一個提高程序與數(shù)據(jù)庫的連接的優(yōu)化,連接池它主要作用是提高性能、節(jié)省資源、控制連接數(shù)、連接管理等操作,這篇文章主要介紹了SpringBoot配置Hikari數(shù)據(jù)庫連接池,需要的朋友可以參考下
    2023-09-09
  • Java中JWT(JSON?Web?Token)的運用具體案例

    Java中JWT(JSON?Web?Token)的運用具體案例

    這篇文章主要介紹了Java中JWT(JSON?Web?Token)的運用具體案例,JWT(JSON?Web?Token)是一種開放標準,用于在網(wǎng)絡應用環(huán)境中安全地傳遞信息,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-11-11
  • Java并發(fā)編程:volatile關(guān)鍵字詳細解析

    Java并發(fā)編程:volatile關(guān)鍵字詳細解析

    這篇文章主要介紹了Java并發(fā)編程:volatile關(guān)鍵字詳細解析,對學習volatile關(guān)鍵字有一定的認識,有需要的可以了解一下。
    2016-11-11
  • IDEA 設置顯示內(nèi)存的使用情況和內(nèi)存回收的方法

    IDEA 設置顯示內(nèi)存的使用情況和內(nèi)存回收的方法

    這篇文章主要介紹了IDEA 設置顯示內(nèi)存的使用情況和內(nèi)存回收的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04

最新評論