Java 8 Instant 時間戳
1. 創建Instant實體,獲取系統的當前時間now
/**
* Java 8 Instant時間戳學習
*/
@Test
public void testInstant(){
// 通過Instant創建Instant實體 回傳:return Clock.systemUTC().instant();
Instant now = Instant.now();
//控制臺輸出:now = 2020-12-29T06:32:49.480Z (以ISO-8601格式輸出)
System.out.println("now = " + now);
}
注意:這里額控制臺輸出:
now = 2020-12-29T06:32:49.480Z,Intance的now方法:
public static Instant now() { return Clock.systemUTC().instant(); }這是輸出的世界標準時間,其中T表示時分秒的開始(或者日期與時間的間隔),Z表示這是一個世界標準時間,
Instant 是時間戳,是指世界標準時格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現在的總秒數,Instant本身實際上是指明時區了,是0時區(也就是比北京時間少8小時),
2. 獲取當前時區的時間(本地時間)
2.1 通過方法Instant.now().atZone(ZoneId.systemDefault())獲取當前地區的時間
ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
輸出結果
2020-12-31T17:31:14.953+08:00[Asia/Shanghai]
2.2 通過增加8小時,轉化為北京時間
| 方法名稱 | 描述 |
|---|---|
| plusMillis() | 增加時間戳時間,以毫秒為單位 |
| minusNanos() | 增加時間戳時間,以納秒為單位 |
| minusSeconds() | 增加時間戳時間,以秒為單位 |
| TimeUnit.HOURS.toMillis() | 將小時轉化為毫秒數 |
//增加8個小時,使Instant.now()回傳時間為北京時間
Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println("now2 = " + now2);
輸出結果:now2 = 2020-12-29T14:35:32.631Z
轉換為符合當前的北京時間,
3. 通過Instant獲取當前時間距離格林威治時間的值
- 通過
getEpochSecond()方法獲取距離格林威治時間的秒數 - 通過
toEpochMilli()方法獲取距離格林威治時間的毫秒數
//增加8個小時,使Instant.now()回傳時間為北京時間
Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
//獲取格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)距離當前時間的秒/毫秒值
System.out.println("距離1970年01月01日00時00分00秒 : "+now2.getEpochSecond() + "秒");
System.out.println("距離1970年01月01日00時00分00秒 : "+now2.toEpochMilli() + "毫秒");
輸出結果:
距離1970年01月01日00時00分00秒 : 1609435201秒
距離1970年01月01日00時00分00秒 : 1609435201645毫秒
4. Instant的from、parse方法
4.1 java.time.Instant.from(TemporalAccessor temporal)原始碼:
public static Instant from(TemporalAccessor temporal) {
if (temporal instanceof Instant) {
return (Instant) temporal;
}
Objects.requireNonNull(temporal, "temporal");
try {
long instantSecs = temporal.getLong(INSTANT_SECONDS);
int nanoOfSecond = temporal.get(NANO_OF_SECOND);
return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
} catch (DateTimeException ex) {
throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
temporal + " of type " + temporal.getClass().getName(), ex);
}
}
引數:temporal 是要轉換的時間物件,回傳的是一個轉換為Instant的瞬間值
如果轉換為Instant的時候失敗,會拋出例外``DateTimeException`
4.2 parse方法原始碼
public static Instant parse(final CharSequence text) {
return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
}
創建自定義的時間戳
//創建自定義的時間戳
System.out.println(Instant.parse("2020-12-29T14:35:32.631Z"));
輸出結果
2020-12-29T14:35:32.631Z
5. Instant的其它常用函式
//獲取當前時間戳
Instant instant = Instant.now();
//獲得當前時間戳并且增加66毫秒
Instant instant1 = Instant.now().plusMillis(66);
//獲得當前時間戳并且減少66毫秒
Instant instant2 = Instant.now().minusMillis(66);
//判斷時間戳 instant 是否在 instant1 之后,回傳boolean
System.out.println(instant.isAfter(instant1)); //回傳false
//判斷時間戳 instant 是否在 instant1 之前,回傳boolean
System.out.println(instant.isBefore(instant1)); //回傳true
//判斷兩個時間戳是否相等, 回傳boolean值
System.out.println(instant.equals(instant1)); //回傳false
//獲得當前時間戳并增加1小時 通過TimeUnit.HOURS.toMillis(1)將小時轉換為毫秒,然后通過plusMillis增加
Instant instant3 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(1));
//獲取時間戳 instant和instant3 相差天數,回傳long型別
//如果小于1天,都算零天,大于等于1天,小于2天算一天
System.out.println("相差天數 = " + instant.until(instant3, ChronoUnit.DAYS)); //回傳0
//獲取時間戳 instant和instant3 相差的小時數,回傳long型別
System.out.println("相差小時 = " + instant.until(instant3, ChronoUnit.HOURS)); //回傳1
//獲取時間戳 instant和instant3 相差的毫秒數,回傳long型別
System.out.println("相差毫秒數 = " + instant.until(instant3, ChronoUnit.MILLIS)); //回傳3600000
輸出結果:
false
true
false
相差天數 = 0
相差小時 = 1
相差毫秒數 = 3600000
6. 將獲取的時間戳轉化為LocalDate
Instant now = Instant.now();
//UTC
ZonedDateTime atZone = now.atZone(ZoneOffset.UTC);
//LocalDateTime
atZone.toLocalDateTime();
LocalDateTime.from(atZone);
//LocalDate
atZone.toLocalDate();
LocalDate date = LocalDate.from(atZone);
//LocalDateTime
atZone.toLocalDateTime();
LocalDateTime.from(date);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243309.html
標籤:java
