Table of Contents
ToggleJava Convert Date to LocalDateTime
可以使用新的 toInstant 方法,此方法已加入到 Java 8 中的 java.util.Date 中,當轉換成 Instant 物件時,需要使用 ZoneId,因為 Instant 物件與時區無關,提供將 Date 實例轉換為 LocalDateTime 實例的多種方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- datetime
| +- DateToLocalDateTimeTest.java
單元測試
Java Date to LocalDateTime 提供 Date 轉換成 LocalDateTime 等操作。
toLocalDateTime
Java Date to LocalDateTime 使用 toInstant 轉換成 LocalDateTime 。
@Test
public void toLocalDateTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 3, 4, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
ZoneId zone = ZoneId.of("Asia/Sakhalin");
LocalDateTime localDateTime = date.toInstant().atZone(zone).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-04-04T03:00", localDateTime.toString());
}
2023-04-04T03:00
toLocalDateTimeWithOfEpochMilli
Java Date to LocalDateTime 使用 Instant.ofEpochMilli 轉換成 LocalDateTime 。
@Test
public void toLocalDateTimeWithOfEpochMilli() {
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 3, 4, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
ZoneId zone = ZoneId.of("Asia/Sakhalin");
LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(zone).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-04-04T03:00", localDateTime.toString());
}
2023-04-04T03:00
toZonedDateTime
Java Date to LocalDateTime 使用 toInstant 轉換成 ZonedDateTime。
@Test
public void toZonedDateTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 3, 4, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
ZoneId zone = ZoneId.of("Asia/Sakhalin");
ZonedDateTime zonedDateTime = date.toInstant().atZone(zone);
System.out.println(zonedDateTime);
assertEquals("2023-04-04T03:00+11:00[Asia/Sakhalin]", zonedDateTime.toString());
}
2023-04-04T03:00+11:00[Asia/Sakhalin]
DateToLocalDateTimeTest.java
Convert Date to LocalDateTime in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.datetime;
import static org.junit.Assert.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
public class DateToLocalDateTime {
@Test
public void toLocalDateTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 3, 4, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
ZoneId zone = ZoneId.of("Asia/Sakhalin");
LocalDateTime localDateTime = date.toInstant().atZone(zone).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-04-04T03:00", localDateTime.toString());
}
@Test
public void toLocalDateTimeWithOfEpochMilli() {
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 3, 4, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
ZoneId zone = ZoneId.of("Asia/Sakhalin");
LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(zone).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-04-04T03:00", localDateTime.toString());
}
@Test
public void toZonedDateTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 3, 4, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
ZoneId zone = ZoneId.of("Asia/Sakhalin");
ZonedDateTime zonedDateTime = date.toInstant().atZone(zone);
System.out.println(zonedDateTime);
assertEquals("2023-04-04T03:00+11:00[Asia/Sakhalin]", zonedDateTime.toString());
}
}
心得分享
Date to LocalDateTime in Java 將 Date 轉換成 LocalDateTime ,Java 8 起,有新的日期 API – java.time ,然而有時仍然需要在新舊 API 之間執行轉換,並使用兩者的日期表示, Java Date LocalDateTime 任務是將該 Date 轉換為 LocalDateTime。