Table of Contents
ToggleJava Convert LocalDate to ZonedDateTime
將 LocalDate 轉換為 ZonedDateTime,在日期後附加時區信息,轉換步驟如下: 1. 取得時區,以便可以將時區資訊附加到日期,2. 調用 atStartOfDay 以便可以將時間附加到日期, Java LocalDate ZonedDateTime 任務是將該 LocalDate 轉換為 ZonedDateTime ,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- datetime
| +- LocalDateToZonedDateTimeTest.java
單元測試
Java LocalDate to ZonedDateTime 提供 LocalDate 轉換成 ZonedDateTime 等操作。
atStartOfDay
Java LocalDate to ZonedDateTime 使用 atStartOfDay 轉換成 ZonedDateTime 。
@Test
public void atStartOfDay() {
LocalDate localDate = LocalDate.of(2023, 4, 4);
System.out.println(localDate);
ZoneId zone = ZoneId.of("Asia/Sakhalin");
ZonedDateTime result = localDate.atStartOfDay(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
localDate = LocalDate.parse("2023-04-04");
System.out.println(localDate);
result = localDate.atStartOfDay(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
}
2023-04-04
2023-04-04T00:00+11:00[Asia/Sakhalin]
2023-04-04
2023-04-04T00:00+11:00[Asia/Sakhalin]
atZone
Java LocalDate to ZonedDateTime 使用 atZone 轉換成 ZonedDateTime 。
@Test
public void atZone() {
LocalDate localDate = LocalDate.of(2023, 4, 4);
System.out.println(localDate);
LocalDateTime localDateTime = localDate.atTime(0, 0, 0);
ZoneId zone = ZoneId.of("Asia/Sakhalin");
ZonedDateTime result = localDateTime.atZone(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
localDate = LocalDate.parse("2023-04-04");
System.out.println(localDate);
result = localDateTime.atZone(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
}
2023-04-04
2023-04-04T00:00+11:00[Asia/Sakhalin]
2023-04-04
2023-04-04T00:00+11:00[Asia/Sakhalin]
LocalDateToZonedDateTimeTest.java
Convert LocalDate to ZonedDateTime in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.datetime;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
public class LocalDateToZonedDateTimeTest {
@Test
public void atStartOfDay() {
LocalDate localDate = LocalDate.of(2023, 4, 4);
System.out.println(localDate);
ZoneId zone = ZoneId.of("Asia/Sakhalin");
ZonedDateTime result = localDate.atStartOfDay(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
localDate = LocalDate.parse("2023-04-04");
System.out.println(localDate);
result = localDate.atStartOfDay(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
}
@Test
public void atZone() {
LocalDate localDate = LocalDate.of(2023, 4, 4);
System.out.println(localDate);
LocalDateTime localDateTime = localDate.atTime(0, 0, 0);
ZoneId zone = ZoneId.of("Asia/Sakhalin");
ZonedDateTime result = localDateTime.atZone(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
localDate = LocalDate.parse("2023-04-04");
System.out.println(localDate);
result = localDateTime.atZone(zone);
System.out.println(result);
assertEquals("2023-04-04T00:00+11:00[Asia/Sakhalin]", result.toString());
}
}
心得分享
LocalDate to ZonedDateTime in Java 將 LocalDate 轉換為 ZonedDateTime 實例的方法,簡單來說過程如: LocalDate -> java.util.ZonedDateTime 這些步驟等, Convert LocalDate to ZonedDateTime in Java 提供將 LocalDate 實例轉換為 ZonedDateTime 實例的方法。