Table of Contents
ToggleJava LocalDateTime Class
新的日期時間 API 跟以往複雜的日期時間 API 有著很大的不同,像是將區域分離,讓物件功能更加專職,簡化了許多日期時間的操作方法,提供增加、減少、比較日期時間、或兩個日期時間差值的方法, LocalDateTime Class 介紹常見的 plus 、 minus 、 isBefore 、 isAfter 、 until 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localdatetime
| +- LocalDateTimeClassTest.java
單元測試
LocalDateTime Class Java 提供日期時間加減、比較前後等操作 LocalDateTime 。
plus
LocalDateTime Class Java 建立一個 LocalDateTime ,提供指定日期時間加法運算。
@Test
public void plus() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.plusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDateTime.plusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(9, month);
result = localDateTime.plusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(4, dayOfMonth);
result = localDateTime.plusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(2, hour);
result = localDateTime.plusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(3, minute);
result = localDateTime.plusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(4, second);
}
2024
9
4
2
3
4
plusTemporalAmount
LocalDateTime Class Java 建立一個 LocalDateTime ,使用 Period 、 Duration 提供指定日期時間加法運算。
@Test
public void plusTemporalAmount() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.plus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDateTime.plus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(9, month);
result = localDateTime.plus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(4, dayOfMonth);
result = localDateTime.plus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(2, hour);
result = localDateTime.plus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(3, minute);
result = localDateTime.plus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(4, second);
}
2024
9
4
2
3
4
minus
LocalDateTime Class Java 建立一個 LocalDateTime ,提供指定日期時間減法運算。
@Test
public void minus() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.minusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDateTime.minusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDateTime.minusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(2, dayOfMonth);
result = localDateTime.minusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(0, hour);
result = localDateTime.minusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(1, minute);
result = localDateTime.minusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(2, second);
}
2022
7
2
0
1
2
minusTemporalAmount
LocalDateTime Class Java 建立一個 LocalDateTime ,使用 Period 、 Duration 提供指定日期時間減法運算。
@Test
public void minusTemporalAmount() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.minus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDateTime.minus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDateTime.minus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(2, dayOfMonth);
result = localDateTime.minus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(0, hour);
result = localDateTime.minus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(1, minute);
result = localDateTime.minus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(2, second);
}
2022
7
2
0
1
2
isBefore
LocalDateTime Class in Java 建立兩個 LocalDateTime ,日期時間比對前後。
@Test
public void isBefore() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime localDateTime2 = LocalDateTime.of(2024, 8, 3, 1, 2, 3);
boolean result = localDateTime.isBefore(localDateTime2);
System.out.println(result);
assertTrue(result);
result = localDateTime2.isBefore(localDateTime);
System.out.println(result);
assertFalse(result);
}
true
false
isAfter
LocalDateTime Class in Java 建立兩個 LocalDateTime ,日期時間比對前後。
@Test
public void isAfter() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime localDateTime2 = LocalDateTime.of(2022, 8, 3, 1, 2, 3);
boolean result = localDateTime.isAfter(localDateTime2);
System.out.println(result);
assertTrue(result);
result = localDateTime2.isAfter(localDateTime);
System.out.println(result);
assertFalse(result);
}
true
false
until
LocalDateTime Class in Java 建立兩個 LocalDateTime ,取得日期時間的差值。
@Test
public void until() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime localDateTime2 = LocalDateTime.of(2023, 8, 4, 1, 2, 3);
long result = localDateTime.until(localDateTime2, ChronoUnit.DAYS);
System.out.println(result);
assertEquals(1, result);
result = localDateTime.until(localDateTime2, ChronoUnit.HOURS);
System.out.println(result);
assertEquals(24, result);
result = localDateTime.until(localDateTime2, ChronoUnit.MINUTES);
System.out.println(result);
assertEquals(1440, result);
}
1
24
1440
atZone
LocalDateTime Class in Java 建立一個 LocalDateTime ,使用 ZoneId 取得指定日期時間 ZonedDateTime。
@Test
public void atZone() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
ZoneId zone = ZoneId.of("UTC+8");
ZonedDateTime result = localDateTime.atZone(zone);
System.out.println(result);
assertEquals("2023-08-03T01:02:03+08:00[UTC+08:00]", result.toString());
}
2023-08-03T01:02:03+08:00[UTC+08:00]
atOffset
LocalDateTime Class in Java 建立一個 LocalDateTime ,使用 ZoneOffset 取得指定日期時間 OffsetDateTime。
@Test
public void atOffset() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
OffsetDateTime result = localDateTime.atOffset(ZoneOffset.ofHours(-4));
System.out.println(result);
assertEquals("2023-08-03T01:02:03-04:00", result.toString());
}
2023-08-03T01:02:03-04:00
LocalDateTimeClassTest.java
LocalDateTime Class in Java 新增單元測試,驗證 Java LocalDateTime Class Example 是否符合預期。
package org.ruoxue.java_147.datetime.localdatetime;
import static org.junit.Assert.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
public class LocalDateTimeClassTest {
@Test
public void plus() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.plusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDateTime.plusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(9, month);
result = localDateTime.plusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(4, dayOfMonth);
result = localDateTime.plusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(2, hour);
result = localDateTime.plusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(3, minute);
result = localDateTime.plusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(4, second);
}
@Test
public void plusTemporalAmount() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.plus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDateTime.plus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(9, month);
result = localDateTime.plus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(4, dayOfMonth);
result = localDateTime.plus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(2, hour);
result = localDateTime.plus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(3, minute);
result = localDateTime.plus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(4, second);
}
@Test
public void minus() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.minusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDateTime.minusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDateTime.minusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(2, dayOfMonth);
result = localDateTime.minusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(0, hour);
result = localDateTime.minusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(1, minute);
result = localDateTime.minusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(2, second);
}
@Test
public void minusTemporalAmount() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime result = localDateTime.minus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDateTime.minus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDateTime.minus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(2, dayOfMonth);
result = localDateTime.minus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(0, hour);
result = localDateTime.minus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(1, minute);
result = localDateTime.minus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(2, second);
}
@Test
public void isBefore() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime localDateTime2 = LocalDateTime.of(2024, 8, 3, 1, 2, 3);
boolean result = localDateTime.isBefore(localDateTime2);
System.out.println(result);
assertTrue(result);
result = localDateTime2.isBefore(localDateTime);
System.out.println(result);
assertFalse(result);
}
@Test
public void isAfter() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime localDateTime2 = LocalDateTime.of(2022, 8, 3, 1, 2, 3);
boolean result = localDateTime.isAfter(localDateTime2);
System.out.println(result);
assertTrue(result);
result = localDateTime2.isAfter(localDateTime);
System.out.println(result);
assertFalse(result);
}
@Test
public void until() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
LocalDateTime localDateTime2 = LocalDateTime.of(2023, 8, 4, 1, 2, 3);
long result = localDateTime.until(localDateTime2, ChronoUnit.DAYS);
System.out.println(result);
assertEquals(1, result);
result = localDateTime.until(localDateTime2, ChronoUnit.HOURS);
System.out.println(result);
assertEquals(24, result);
result = localDateTime.until(localDateTime2, ChronoUnit.MINUTES);
System.out.println(result);
assertEquals(1440, result);
}
@Test
public void atZone() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
ZoneId zone = ZoneId.of("UTC+8");
ZonedDateTime result = localDateTime.atZone(zone);
System.out.println(result);
assertEquals("2023-08-03T01:02:03+08:00[UTC+08:00]", result.toString());
}
@Test
public void atOffset() {
LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 3, 1, 2, 3);
OffsetDateTime result = localDateTime.atOffset(ZoneOffset.ofHours(-4));
System.out.println(result);
assertEquals("2023-08-03T01:02:03-04:00", result.toString());
}
}
心得分享
Java LocalDateTime Class Example 屬於 java.time ,物件的值不會被改變,可以同時在不同的執行緒下被使用, LocalDateTime Class in Java 提供 plus 、 minus 、 isBefore 、 isAfter 等操作範例。