JavaSE常用類之Instant類及常用方法

JavaSE常用類之Instant類及常用方法,第1張

Instant 類的使用

Instant 類概述

在時間線上的瞬間點。該在時間線上建立單個瞬時點。 這可能用於在應用程序中記錄事件時間戳。

now( ) 方法

//now() 獲取本初子午線的時間
Instant instant = Instant.now();
System.out.println(instant);      //中時區2021-03-09T02:02:46.596Z

atOffset( ) 方法添加時間偏移量

//添加時間偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);       //東八區2021-03-09T10:02:46.596 08:00

toEpochMilli( ) 方法獲取時間戳

long milli = instant.toEpochMilli();
System.out.println(milli);//1615255366596

ofEpochMilli( ) 方法通過時間戳獲取時間

//通過時間戳獲取時間 ----- 本初子午線的時間
Instant instant1 = Instant.ofEpochMilli(1615198793505L);
System.out.println(instant1);//2021-03-08T10:19:53.505Z

生活常識_百科知識_各類知識大全»JavaSE常用類之Instant類及常用方法

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情