Table of Contents
ToggleLocalDateTime in Java with Examples
區分了時間戳、本地日期、本地時間和帶時區的日期時間,LocalDateTime in Java 介紹常見的 format 、 parse 、 ofInstant 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localdatetime
| +- LocalDateTimeWithExamplesTest.java
單元測試
LocalDateTime Java 提供日期時間建立、格式化等操作 LocalDateTime 。
format
建立一個 LocalDateTime ,格式化指定日期時間。
@Test
public void format() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
String result = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result);
localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
result = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(result);
assertEquals("2023-08-03T01:02:03.123456789", result);
result = localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS"));
System.out.println(result);
assertEquals("2023/08/03 01:02:03.123", result);
}
2023-08-03T01:02:03
2023-08-03T01:02:03.123456789
2023/08/03 01:02:03.123
parse
LocalDateTime Java 建立一個 LocalDateTime ,使用 String 建立指定日期時間。
@Test
public void parse() {
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-03T01:02:03");
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
localDateTime = LocalDateTime.parse("2023-08-03T01:02:03.123456789");
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123456789", localDateTime.toString());
localDateTime = LocalDateTime.parse("2023/08/03 01:02:03.123", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS"));
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123", localDateTime.toString());
}
2023-08-03T01:02:03
2023-08-03T01:02:03.123456789
2023-08-03T01:02:03.123
from
LocalDateTime Java 建立一個 LocalDateTime ,從另一個日期時間,建立指定日期時間。
@Test
public void from() {
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-03T01:02:03");
LocalDateTime result = LocalDateTime.from(localDateTime);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result.toString());
localDateTime = LocalDateTime.parse("2023-08-03T01:02:03");
ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);
System.out.println(offsetDateTime);
result = LocalDateTime.from(offsetDateTime);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result.toString());
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-08-03T01:02:03+09:00[Asia/Tokyo]");
result = LocalDateTime.from(zonedDateTime);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result.toString());
}
2023-08-03T01:02:03
2023-08-03T01:02:03+09:00
2023-08-03T01:02:03
2023-08-03T01:02:03
ofInstant
LocalDateTime Java 建立一個 LocalDateTime ,使用 Instant 建立指定日期時間。
@Test
public void ofInstant() {
ZoneId zone = ZoneId.of("UTC+9");
Instant instant = Instant.ofEpochSecond(1690992123L);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
instant = Instant.ofEpochSecond(1690992123L, 123456789);
localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123456789", localDateTime.toString());
}
2023-08-03T01:02:03
2023-08-03T01:02:03.123456789
toEpochSecond
Java LocalDateTime 建立一個 LocalDateTime ,取得紀元秒,從 UTC 的 1970-01-01T00:00:00Z 起。
@Test
public void toEpochSecond() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
long result = localDateTime.toEpochSecond(zoneOffset);
System.out.println(result);
assertEquals(1690992123L, result);
localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
zoneOffset = ZoneOffset.of("+09:00");
result = localDateTime.toEpochSecond(zoneOffset);
System.out.println(result);
assertEquals(1690992123L, result);
}
1690992123
1690992123
toEpochMilli
Java LocalDateTime 建立一個 LocalDateTime ,取得紀元毫秒,從 UTC 的 1970-01-01T00:00:00Z 起。
@Test
public void toEpochMilli() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
ZoneId zone = ZoneId.of("UTC+9");
Instant instant = localDateTime.atZone(zone).toInstant();
long result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1690992123000L, result);
localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
zone = ZoneId.of("UTC+9");
instant = localDateTime.atZone(zone).toInstant();
result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1690992123123L, result);
}
1690992123000
1690992123123
ofEpochSecond
Java LocalDateTime 建立一個 LocalDateTime ,將紀元秒轉成指定日期時間。
@Test
public void ofEpochSecond() {
ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(1690992123L, 0, zoneOffset);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
localDateTime = LocalDateTime.ofEpochSecond(1690992123L, 123456789, zoneOffset);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123456789", localDateTime.toString());
}
2023-08-03T01:02:03
2023-08-03T01:02:03.123456789
ofEpochMilli
Java LocalDateTime 建立一個 LocalDateTime ,將紀元毫秒轉成指定日期時間
@Test
public void ofEpochMilli() {
ZoneId zone = ZoneId.of("UTC+9");
Instant instant = Instant.ofEpochMilli(1690992123000L);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
instant = Instant.ofEpochMilli(1690992123123L);
localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123", localDateTime.toString());
}
2023-08-03T01:02:03
2023-08-03T01:02:03.123
timestamp
Java LocalDateTime 建立一個 LocalDateTime ,使用 Timestamp 建立指定日期時間。
@Test
public void timestamp() {
LocalDateTime localDateTime = new Timestamp(1690995723000L).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
localDateTime = new Timestamp(1690995723123L).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123", localDateTime.toString());
}
2023-08-03T01:02:03
2023-08-03T01:02:03.123
truncatedTo
Java LocalDateTime 建立一個 LocalDateTime ,捨棄欄位到指定欄位。
@Test
public void truncatedTo() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
LocalDateTime result = localDateTime.truncatedTo(ChronoUnit.HOURS);
System.out.println(result);
assertEquals("2023-08-03T01:00", result.toString());
result = localDateTime.truncatedTo(ChronoUnit.DAYS);
System.out.println(result);
assertEquals("2023-08-03T00:00", result.toString());
result = localDateTime.truncatedTo(ChronoUnit.MILLIS);
System.out.println(result);
assertEquals("2023-08-03T01:02:03.123", result.toString());
}
2023-08-03T01:00
2023-08-03T00:00
2023-08-03T01:02:03.123
LocalDateTimeWithExamplesTest.java
LocalDateTime in Java 新增單元測試,驗證 Java LocalDateTime Example 是否符合預期。
package org.ruoxue.java_147.datetime.localdatetime;
import static org.junit.Assert.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
public class LocalDateTimeWithExamplesTest {
@Test
public void format() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
String result = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result);
localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
result = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(result);
assertEquals("2023-08-03T01:02:03.123456789", result);
result = localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS"));
System.out.println(result);
assertEquals("2023/08/03 01:02:03.123", result);
}
@Test
public void parse() {
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-03T01:02:03");
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
localDateTime = LocalDateTime.parse("2023-08-03T01:02:03.123456789");
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123456789", localDateTime.toString());
localDateTime = LocalDateTime.parse("2023/08/03 01:02:03.123",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS"));
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123", localDateTime.toString());
}
@Test
public void from() {
LocalDateTime localDateTime = LocalDateTime.parse("2023-08-03T01:02:03");
LocalDateTime result = LocalDateTime.from(localDateTime);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result.toString());
localDateTime = LocalDateTime.parse("2023-08-03T01:02:03");
ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);
System.out.println(offsetDateTime);
result = LocalDateTime.from(offsetDateTime);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result.toString());
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-08-03T01:02:03+09:00[Asia/Tokyo]");
result = LocalDateTime.from(zonedDateTime);
System.out.println(result);
assertEquals("2023-08-03T01:02:03", result.toString());
}
@Test
public void ofInstant() {
ZoneId zone = ZoneId.of("UTC+9");
Instant instant = Instant.ofEpochSecond(1690992123L);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
instant = Instant.ofEpochSecond(1690992123L, 123456789);
localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123456789", localDateTime.toString());
}
@Test
public void toEpochSecond() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
long result = localDateTime.toEpochSecond(zoneOffset);
System.out.println(result);
assertEquals(1690992123L, result);
localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
zoneOffset = ZoneOffset.of("+09:00");
result = localDateTime.toEpochSecond(zoneOffset);
System.out.println(result);
assertEquals(1690992123L, result);
}
@Test
public void toEpochMilli() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
ZoneId zone = ZoneId.of("UTC+9");
Instant instant = localDateTime.atZone(zone).toInstant();
long result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1690992123000L, result);
localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
zone = ZoneId.of("UTC+9");
instant = localDateTime.atZone(zone).toInstant();
result = instant.toEpochMilli();
System.out.println(result);
assertEquals(1690992123123L, result);
}
@Test
public void ofEpochSecond() {
ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(1690992123L, 0, zoneOffset);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
localDateTime = LocalDateTime.ofEpochSecond(1690992123L, 123456789, zoneOffset);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123456789", localDateTime.toString());
}
@Test
public void ofEpochMilli() {
ZoneId zone = ZoneId.of("UTC+9");
Instant instant = Instant.ofEpochMilli(1690992123000L);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
instant = Instant.ofEpochMilli(1690992123123L);
localDateTime = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123", localDateTime.toString());
}
@Test
public void timestamp() {
LocalDateTime localDateTime = new Timestamp(1690995723000L).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03", localDateTime.toString());
localDateTime = new Timestamp(1690995723123L).toLocalDateTime();
System.out.println(localDateTime);
assertEquals("2023-08-03T01:02:03.123", localDateTime.toString());
}
@Test
public void truncatedTo() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3, 123456789);
LocalDateTime result = localDateTime.truncatedTo(ChronoUnit.HOURS);
System.out.println(result);
assertEquals("2023-08-03T01:00", result.toString());
result = localDateTime.truncatedTo(ChronoUnit.DAYS);
System.out.println(result);
assertEquals("2023-08-03T00:00", result.toString());
result = localDateTime.truncatedTo(ChronoUnit.MILLIS);
System.out.println(result);
assertEquals("2023-08-03T01:02:03.123", result.toString());
}
}
心得分享
Java LocalDateTime Example 提供了一組日期和時間相關的 API ,物件的值不會被改變,可以同時在不同的執行緒下被使用,提供了幾種 LocalDateTime 常見方法的操作範例,在應用上相當廣泛,熟悉 Java LocalDateTime 這些方法的操作,像是: format 、 parse 、 ofInstant 等方法。