Table of Contents
ToggleJava LocalDate Class
新的日期 API 跟以往複雜的日期 API 有著很大的不同,像是將區域分離,讓物件功能更加專職,簡化了許多日期的操作方法,提供增加、減少、比較日期、或兩個日期差值的方法, LocalDate Class 介紹常見的 plus 、 minus 、 isBefore 、 isAfter 、 until 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localdate
| +- LocalDateClassTest.java
單元測試
LocalDate Class Java 提供日期加減、比較前後等操作 LocalDate 。
plus
LocalDate Class Java 建立一個 LocalDate ,提供指定日期加法運算。
@Test
public void plus() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.plusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDate.plusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDate.plusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(19, dayOfMonth);
}
2024
7
19
plusTemporalAmount
LocalDate Class Java 建立一個 LocalDate ,使用 Period 提供指定日期加法運算。
@Test
public void plusTemporalAmount() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.plus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDate.plus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDate.plus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(19, dayOfMonth);
}
2024
7
19
minus
LocalDate Class Java 建立一個 LocalDate ,提供指定日期減法運算。
@Test
public void minus() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.minusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDate.minusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(5, month);
result = localDate.minusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(17, dayOfMonth);
}
2022
5
17
minusTemporalAmount
LocalDate Class Java 建立一個 LocalDate ,使用 Period 提供指定日期減法運算。
@Test
public void minusTemporalAmount() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.minus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDate.minus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(5, month);
result = localDate.minus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(17, dayOfMonth);
}
2022
5
17
isBefore
LocalDate Class in Java 建立兩個 LocalDate ,日期比對前後。
@Test
public void isBefore() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
boolean result = localDate.isBefore(localDate2);
System.out.println(result);
assertTrue(result);
result = localDate2.isBefore(localDate);
System.out.println(result);
assertFalse(result);
}
true
false
isAfter
LocalDate Class in Java 建立兩個 LocalDate ,日期比對前後。
@Test
public void isAfter() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2022, 6, 18);
boolean result = localDate.isAfter(localDate2);
System.out.println(result);
assertTrue(result);
result = localDate2.isAfter(localDate);
System.out.println(result);
assertFalse(result);
}
true
false
until
LocalDate Class in Java 建立兩個 LocalDate ,取得日期的差值。
@Test
public void until() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
long result = localDate.until(localDate2, ChronoUnit.YEARS);
System.out.println(result);
assertEquals(1, result);
result = localDate.until(localDate2, ChronoUnit.MONTHS);
System.out.println(result);
assertEquals(12, result);
result = localDate.until(localDate2, ChronoUnit.DAYS);
System.out.println(result);
assertEquals(366, result);
}
1
12
366
atStartOfDay
LocalDate Class in Java 建立一個 LocalDate ,使用 ZoneId 取得指定日期 ZonedDateTime。
@Test
public void atStartOfDay() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
ZoneId zone = ZoneId.of("UTC+8");
ZonedDateTime result = localDate.atStartOfDay(zone);
System.out.println(result);
assertEquals("2023-06-18T00:00+08:00[UTC+08:00]", result.toString());
}
2023-06-18T00:00+08:00[UTC+08:00]
LocalDateClassTest.java
LocalDate Class in Java 新增單元測試,驗證 Java LocalDate Class Example 是否符合預期。
package org.ruoxue.java_147.datetime.localdate;
import static org.junit.Assert.*;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
public class LocalDateClassTest {
@Test
public void plus() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.plusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDate.plusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDate.plusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(19, dayOfMonth);
}
@Test
public void plusTemporalAmount() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.plus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2024, year);
result = localDate.plus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(7, month);
result = localDate.plus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(19, dayOfMonth);
}
@Test
public void minus() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.minusYears(1);
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDate.minusMonths(1);
int month = result.getMonthValue();
System.out.println(month);
assertEquals(5, month);
result = localDate.minusDays(1);
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(17, dayOfMonth);
}
@Test
public void minusTemporalAmount() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate result = localDate.minus(Period.ofYears(1));
int year = result.getYear();
System.out.println(year);
assertEquals(2022, year);
result = localDate.minus(Period.ofMonths(1));
int month = result.getMonthValue();
System.out.println(month);
assertEquals(5, month);
result = localDate.minus(Period.ofDays(1));
int dayOfMonth = result.getDayOfMonth();
System.out.println(dayOfMonth);
assertEquals(17, dayOfMonth);
}
@Test
public void isBefore() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
boolean result = localDate.isBefore(localDate2);
System.out.println(result);
assertTrue(result);
result = localDate2.isBefore(localDate);
System.out.println(result);
assertFalse(result);
}
@Test
public void isAfter() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2022, 6, 18);
boolean result = localDate.isAfter(localDate2);
System.out.println(result);
assertTrue(result);
result = localDate2.isAfter(localDate);
System.out.println(result);
assertFalse(result);
}
@Test
public void until() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
long result = localDate.until(localDate2, ChronoUnit.YEARS);
System.out.println(result);
assertEquals(1, result);
result = localDate.until(localDate2, ChronoUnit.MONTHS);
System.out.println(result);
assertEquals(12, result);
result = localDate.until(localDate2, ChronoUnit.DAYS);
System.out.println(result);
assertEquals(366, result);
}
@Test
public void atStartOfDay() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
ZoneId zone = ZoneId.of("UTC+8");
ZonedDateTime result = localDate.atStartOfDay(zone);
System.out.println(result);
assertEquals("2023-06-18T00:00+08:00[UTC+08:00]", result.toString());
}
}
心得分享
Java LocalDate Class Example 屬於 java.time ,物件的值不會被改變,可以同時在不同的執行緒下被使用, LocalDate Class in Java 提供 plus 、 minus 、 isBefore 、 isAfter 、 until 等操作範例。