Table of Contents
ToggleInitialize ZonedDateTime in Java
初始化 ZonedDateTime 物件的值不會被改變,可以同時在不同的執行緒下被使用,介紹常見的 of 、 parse 、 ofEpochMilli 等方法,了解 Initialize ZonedDateTime Java 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- zoneddatetime
| +- InitializeZonedDateTimeTest.java
單元測試
Java Initialize ZonedDateTime 提供初始化操作 ZonedDateTime 。
now
建立一個 ZonedDateTime ,提供目前日期時間。
@Test
public void now() {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
ZoneId zone = ZoneId.of("Europe/Athens");
zonedDateTime = ZonedDateTime.now(zone);
System.out.println(zonedDateTime);
zone = ZoneId.of("UTC+3");
Clock clock = Clock.system(zone);
zonedDateTime = ZonedDateTime.now(clock);
System.out.println(zonedDateTime);
zone = ZoneId.of("Etc/GMT-3");
zonedDateTime = ZonedDateTime.now(zone);
System.out.println(zonedDateTime);
}
2023-07-05T14:30:16.052+08:00[Asia/Taipei]
2023-07-05T09:30:16.056+03:00[Europe/Athens]
2023-07-05T09:30:16.078+03:00[UTC+03:00]
2023-07-05T09:30:16.078+03:00[Etc/GMT-3]
of
建立一個 ZonedDateTime ,建立指定日期時間。
@Test
public void of() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
long epochSecond = zonedDateTime.toEpochSecond();
System.out.println(epochSecond);
assertEquals(1686024489L, epochSecond);
zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.of(LocalDate.of(2023, 6, 6), LocalTime.of(7, 8, 9, 0), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
}
2023-06-06T07:08:09+03:00[Europe/Athens]
1686024489
2023-06-06T07:08:09+03:00[Europe/Athens]
2023-06-06T07:08:09+03:00[Europe/Athens]
ofInstant
Java Initialize ZonedDateTime 建立一個 ZonedDateTime ,使用 Instant 建立指定日期時間。
@Test
public void ofInstant() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.parse("2023-06-06T04:08:09Z"), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.ofInstant(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), ZoneOffset.ofHours(3), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
}
2023-06-06T07:08:09+03:00[Europe/Athens]
2023-06-06T07:08:09+03:00[Europe/Athens]
ofLocal
Java Initialize ZonedDateTime 建立一個 ZonedDateTime ,建立指定日期時間。
@Test
public void ofLocal() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone,
ZoneOffset.ofHours(3));
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
}
2023-06-06T07:08:09+03:00[Europe/Athens]
parse
Java Initialize ZonedDateTime 建立一個 ZonedDateTime ,建立指定日期時間。
@Test
public void parse() {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-06-06T07:08:09+03:00[Europe/Athens]");
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.parse("2023-06-06T07:08:09.123456789+03:00[Europe/Athens]");
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09.123456789+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.parse("2023/06/06 07:08:09.123+0300",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSSZ"));
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09.123+03:00", zonedDateTime.toString());
}
2023-06-06T07:08:09+03:00[Europe/Athens]
2023-06-06T07:08:09.123456789+03:00[Europe/Athens]
2023-06-06T07:08:09.123+03:00
from
ZonedDateTime Initialization Java 建立一個 ZonedDateTime ,從另一個日期時間,建立指定日期時間。
@Test
public void from() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
ZonedDateTime result = ZonedDateTime.from(zonedDateTime);
System.out.println(result);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", result.toString());
LocalDateTime localDateTime = LocalDateTime.parse("2023-06-06T07:08:09");
ZoneOffset zoneOffset = ZoneOffset.of("+03:00");
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);
System.out.println(offsetDateTime);
result = ZonedDateTime.from(offsetDateTime);
System.out.println(result);
assertEquals("2023-06-06T07:08:09+03:00", result.toString());
offsetDateTime = OffsetDateTime.of(2023, 6, 6, 7, 8, 9, 0, zoneOffset);
result = ZonedDateTime.from(offsetDateTime);
System.out.println(result);
assertEquals("2023-06-06T07:08:09+03:00", result.toString());
}
2023-06-06T07:08:09+03:00[Europe/Athens]
2023-06-06T07:08:09+03:00
2023-06-06T07:08:09+03:00
2023-06-06T07:08:09+03:00
ofEpochMilli
ZonedDateTime Initialization Java 建立一個 ZonedDateTime ,將紀元毫秒轉成指定日期時間。
@Test
public void ofEpochMilli() {
ZoneId zone = ZoneId.of("UTC+3");
Instant instant = Instant.ofEpochMilli(1686024489000L);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[UTC+03:00]", zonedDateTime.toString());
instant = Instant.ofEpochMilli(1686024489123L);
zonedDateTime = ZonedDateTime.ofInstant(instant, zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09.123+03:00[UTC+03:00]", zonedDateTime.toString());
}
2023-06-06T07:08:09+03:00[UTC+03:00]
2023-06-06T07:08:09.123+03:00[UTC+03:00]
InitializeZonedDateTimeTest.java
ZonedDateTime Initialization Java 新增單元測試,驗證 Java ZonedDateTime Initialize 是否符合預期。
package org.ruoxue.java_147.datetime.zoneddatetime;
import static org.junit.Assert.*;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.Test;
public class InitializeZonedDateTimeTest {
@Test
public void now() {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
ZoneId zone = ZoneId.of("Europe/Athens");
zonedDateTime = ZonedDateTime.now(zone);
System.out.println(zonedDateTime);
zone = ZoneId.of("UTC+3");
Clock clock = Clock.system(zone);
zonedDateTime = ZonedDateTime.now(clock);
System.out.println(zonedDateTime);
zone = ZoneId.of("Etc/GMT-3");
zonedDateTime = ZonedDateTime.now(zone);
System.out.println(zonedDateTime);
}
@Test
public void of() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
long epochSecond = zonedDateTime.toEpochSecond();
System.out.println(epochSecond);
assertEquals(1686024489L, epochSecond);
zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.of(LocalDate.of(2023, 6, 6), LocalTime.of(7, 8, 9, 0), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
}
@Test
public void ofInstant() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.parse("2023-06-06T04:08:09Z"), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.ofInstant(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), ZoneOffset.ofHours(3), zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
}
@Test
public void ofLocal() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone,
ZoneOffset.ofHours(3));
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
}
@Test
public void parse() {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-06-06T07:08:09+03:00[Europe/Athens]");
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.parse("2023-06-06T07:08:09.123456789+03:00[Europe/Athens]");
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09.123456789+03:00[Europe/Athens]", zonedDateTime.toString());
zonedDateTime = ZonedDateTime.parse("2023/06/06 07:08:09.123+0300",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSSZ"));
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09.123+03:00", zonedDateTime.toString());
}
@Test
public void from() {
ZoneId zone = ZoneId.of("Europe/Athens");
ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
ZonedDateTime result = ZonedDateTime.from(zonedDateTime);
System.out.println(result);
assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", result.toString());
LocalDateTime localDateTime = LocalDateTime.parse("2023-06-06T07:08:09");
ZoneOffset zoneOffset = ZoneOffset.of("+03:00");
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);
System.out.println(offsetDateTime);
result = ZonedDateTime.from(offsetDateTime);
System.out.println(result);
assertEquals("2023-06-06T07:08:09+03:00", result.toString());
offsetDateTime = OffsetDateTime.of(2023, 6, 6, 7, 8, 9, 0, zoneOffset);
result = ZonedDateTime.from(offsetDateTime);
System.out.println(result);
assertEquals("2023-06-06T07:08:09+03:00", result.toString());
}
@Test
public void ofEpochMilli() {
ZoneId zone = ZoneId.of("UTC+3");
Instant instant = Instant.ofEpochMilli(1686024489000L);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09+03:00[UTC+03:00]", zonedDateTime.toString());
instant = Instant.ofEpochMilli(1686024489123L);
zonedDateTime = ZonedDateTime.ofInstant(instant, zone);
System.out.println(zonedDateTime);
assertEquals("2023-06-06T07:08:09.123+03:00[UTC+03:00]", zonedDateTime.toString());
}
}
心得分享
Java ZonedDateTime Initialize 是一個表示帶有時區訊息的日期和時間的類別,位於ISO-8601日曆系統中,是不可變的物件,所有計算都會建立一個新的物件,在多執行緒中使用是安全的,ZonedDateTime Initialization Java 提供了幾種 ZonedDateTime 初始化的操作範例,使用單元測試驗證產出結果。