Table of Contents
ToggleJava Convert LocalDateTime to Date
將 LocalDate 轉換為 Date 實例的方法,簡單來說過程如: LocalDate -> ZonedDateTime -> Instant -> java.util.Date 這些步驟等, Java LocalDateTime Date 任務是將該 LocalDateTime 轉換為 Date ,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- datetime
| +- LocalDateTimeToDateTest.java
單元測試
Java LocalDateTime to Date 提供 LocalDateTime 轉換成 Date 等操作。
toDate
Java LocalDateTime to Date 使用 atStartOfDay 轉換成 Date 。
@Test
public void toDate() {
ZoneId zone = ZoneId.of("Asia/Sakhalin");
LocalDateTime localDateTime = LocalDateTime.of(2023, 4, 4, 0, 0, 0);
ZonedDateTime zonedDateTime = localDateTime.atZone(zone);
Instant instant = zonedDateTime.toInstant();
Date date = Date.from(instant);
System.out.println(date);
assertEquals("Mon Apr 03 21:00:00 CST 2023", date.toString());
}
Mon Apr 03 21:00:00 CST 2023
toDateWithToInstant
Java LocalDateTime to Date 使用 toInstant 轉換成 Date 。
@Test
public void toDateWithToInstant() {
ZoneId zone = ZoneId.of("Etc/GMT-11");
LocalDateTime localDateTime = LocalDateTime.of(2023, 4, 4, 0, 0, 0);
ZonedDateTime zonedDateTime = localDateTime.atZone(zone);
Instant instant = zonedDateTime.toInstant();
Date date = Date.from(instant);
System.out.println(date);
assertEquals("Mon Apr 03 21:00:00 CST 2023", date.toString());
}
Mon Apr 03 21:00:00 CST 2023
toDateWithValueOf
Java LocalDateTime to Date 使用 valueOf 轉換成 Date 。
@Test
public void toDateWithValueOf() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 4, 4, 0, 0, 0);
String value = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Instant instant = Timestamp.valueOf(value).toInstant();
Date date = Date.from(instant);
System.out.println(date);
assertEquals("Tue Apr 04 00:00:00 CST 2023", date.toString());
}
Tue Apr 04 00:00:00 CST 2023
LocalDateTimeToDateTest.java
Convert LocalDateTime to Date in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.datetime;
import static org.junit.Assert.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.junit.Test;
public class LocalDateTimeToDate {
@Test
public void toDate() {
ZoneId zone = ZoneId.of("Asia/Sakhalin");
LocalDateTime localDateTime = LocalDateTime.of(2023, 4, 4, 0, 0, 0);
ZonedDateTime zonedDateTime = localDateTime.atZone(zone);
Instant instant = zonedDateTime.toInstant();
Date date = Date.from(instant);
System.out.println(date);
assertEquals("Mon Apr 03 21:00:00 CST 2023", date.toString());
}
@Test
public void toDateWithToInstant() {
ZoneId zone = ZoneId.of("Etc/GMT-11");
LocalDateTime localDateTime = LocalDateTime.of(2023, 4, 4, 0, 0, 0);
ZonedDateTime zonedDateTime = localDateTime.atZone(zone);
Instant instant = zonedDateTime.toInstant();
Date date = Date.from(instant);
System.out.println(date);
assertEquals("Mon Apr 03 21:00:00 CST 2023", date.toString());
}
@Test
public void toDateWithValueOf() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 4, 4, 0, 0, 0);
String value = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Instant instant = Timestamp.valueOf(value).toInstant();
Date date = Date.from(instant);
System.out.println(date);
assertEquals("Tue Apr 04 00:00:00 CST 2023", date.toString());
}
}
心得分享
LocalDateTime to Date in Java 將 LocalDate 轉換為 Date,在日期後附加時間和時區信息,轉換步驟如下: 1. 取得時區,以便可以將時區資訊附加到日期,2. 調用 atStartOfDay() 以便可以將時間附加到日期, 3. 本地日期 + atStartOfDay() + 時區 + toInstant(), Convert LocalDateTime to Date in Java 提供將 LocalDateTime 實例轉換為 Date 實例的多種方法。