Table of Contents
ToggleInitialize Instant in Java
初始化 Instant 時間戳代表的是某個時間,其值計算是從 1970 年 1 月 1 日(1970-01-01T00:00:00Z)第一秒開始的時間,也稱為 EPOCH。發生在時期之前的時間戳具有負值,發生在時期後的時間戳具有正值,1970-01-01T00:00:00Z 中的 Z 其實就是偏移量為 0,介紹常見的 of 、 parse 、 ofEpochMilli 等方法,了解 Initialize Instant Java 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- instant
| +- InitializeInstantTest.java
單元測試
Java Initialize Instant 提供初始化操作 Instant 。
now
建立一個 Instant ,提供目前時間戳。
@Test
public void now() {
Instant instant = Instant.now();
System.out.println(instant);
ZoneId zone = ZoneId.of("America/New_York");
Clock clock = Clock.system(zone);
instant = Instant.now(clock);
System.out.println(instant);
zone = ZoneId.of("UTC-4");
clock = Clock.system(zone);
instant = Instant.now(clock);
System.out.println(instant);
}
2023-06-22T11:47:17.745Z
2023-06-22T11:47:17.788Z
2023-06-22T11:47:17.788Z
parse
Java Initialize Instant 建立一個 Instant ,建立指定時間戳。
@Test
public void parse() {
Instant instant = Instant.parse("2023-09-12T04:05:06.123456789Z");
long result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1694491506123L, result);
instant = Instant.parse("2023-09-12T04:05:06Z");
result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1694491506000L, result);
}
1694491506123
1694491506000
from
Instant Initialization Java 建立一個 Instant ,從另一個時間戳,建立指定時間戳。
@Test
public void from() {
Instant instant = Instant.parse("2023-09-12T04:05:06.123456789Z");
Instant result = Instant.from(instant);
System.out.println(result);
assertEquals("2023-09-12T04:05:06.123456789Z", result.toString());
instant = Instant.parse("2023-09-12T04:05:06Z");
result = Instant.from(instant);
System.out.println(result);
assertEquals("2023-09-12T04:05:06Z", result.toString());
}
2023-09-12T04:05:06.123456789Z
2023-09-12T04:05:06Z
ofEpochSecond
Instant Initialization Java 建立一個 Instant ,將紀元秒轉成指定時間戳。
@Test
public void ofEpochSecond() {
Instant instant = Instant.ofEpochSecond(1694491506L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06Z", instant.toString());
instant = Instant.ofEpochSecond(1694491506L, 123456789L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06.123456789Z", instant.toString());
}
2023-09-12T04:05:06Z
2023-09-12T04:05:06.123456789Z
ofEpochMilli
Instant Initialization Java 建立一個 Instant ,將紀元毫秒轉成指定時間戳。
@Test
public void ofEpochMilli() {
Instant instant = Instant.ofEpochMilli(1694491506123L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06.123Z", instant.toString());
instant = Instant.ofEpochMilli(1694491506000L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06Z", instant.toString());
}
2023-09-12T04:05:06.123Z
2023-09-12T04:05:06Z
InitializeInstantTest.java
Instant Initialization Java 新增單元測試,驗證 Java Instant Initialize 是否符合預期。
package org.ruoxue.java_147.datetime.instant;
import static org.junit.Assert.*;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import org.junit.Test;
public class InitializeInstantTest {
@Test
public void now() {
Instant instant = Instant.now();
System.out.println(instant);
ZoneId zone = ZoneId.of("America/New_York");
Clock clock = Clock.system(zone);
instant = Instant.now(clock);
System.out.println(instant);
zone = ZoneId.of("UTC-4");
clock = Clock.system(zone);
instant = Instant.now(clock);
System.out.println(instant);
}
@Test
public void parse() {
Instant instant = Instant.parse("2023-09-12T04:05:06.123456789Z");
long result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1694491506123L, result);
instant = Instant.parse("2023-09-12T04:05:06Z");
result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1694491506000L, result);
}
@Test
public void from() {
Instant instant = Instant.parse("2023-09-12T04:05:06.123456789Z");
Instant result = Instant.from(instant);
System.out.println(result);
assertEquals("2023-09-12T04:05:06.123456789Z", result.toString());
instant = Instant.parse("2023-09-12T04:05:06Z");
result = Instant.from(instant);
System.out.println(result);
assertEquals("2023-09-12T04:05:06Z", result.toString());
}
@Test
public void ofEpochSecond() {
Instant instant = Instant.ofEpochSecond(1694491506L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06Z", instant.toString());
instant = Instant.ofEpochSecond(1694491506L, 123456789L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06.123456789Z", instant.toString());
}
@Test
public void ofEpochMilli() {
Instant instant = Instant.ofEpochMilli(1694491506123L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06.123Z", instant.toString());
instant = Instant.ofEpochMilli(1694491506000L);
System.out.println(instant);
assertEquals("2023-09-12T04:05:06Z", instant.toString());
}
}
心得分享
Java Instant Initialize 屬於 java.time ,在時間軸中代表 date 和 time,精確到奈秒,物件的值不會被改變,可以同時在不同的執行緒下被使用,Instant Initialization Java 提供了幾種 Instant 初始化的操作範例,使用單元測試驗證產出結果。