Table of Contents
ToggleCompare LocalDates in Java
介紹一些常見的方法,像是: compareTo 、 isBefore 、 isAfter 、 isEqual 等方法,比較日期與另一個日期,檢查前後或相等的日期, Java Compare LocalDates 屬於 java.time 套件,日期和時間相關的 API ,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localdate
| +- CompareLocalDatesTest.java
單元測試
Compare LocalDate Instances 提供比較兩個 LocalDate 物件的方法。
compareTo
Compare LocalDateTime Instances 建立兩個 LocalDateTime ,比較日期時間物件,根據比較結果傳回一個整數值,不考慮時區或日曆資訊。
指定的日期時間早於另一個日期時間,則為負整數,如: a 小於 b 傳回 -1 。
指定的日期時間等於另一個日期時間,則為零,如: a 等於 b 傳回 0 。
指定的日期時間晚於另一個日期時間,則為正整數,如: a 大於 b 傳回 1 。
@Test
public void compareTo() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
int result = localDate.compareTo(localDate2);
System.out.println(result);
assertThat(result).isLessThan(0);
localDate = LocalDate.of(2023, 6, 18);
localDate2 = LocalDate.of(2023, 6, 18);
result = localDate.compareTo(localDate2);
System.out.println(result);
assertThat(result).isEqualTo(0);
localDate = LocalDate.of(2023, 6, 18);
localDate2 = LocalDate.of(2022, 6, 18);
result = localDate.compareTo(localDate2);
System.out.println(result);
assertThat(result).isGreaterThan(0);
}
-1
0
1
isBefore
Compare LocalDate Instances 建立兩個 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);
assertThat(result).isTrue();
result = localDate2.isBefore(localDate);
System.out.println(result);
assertThat(result).isFalse();
}
true
false
isAfter
Compare LocalDate Instances 建立兩個 LocalDate ,檢查指定的日期是否晚於另一個日期。
@Test
public void isAfter() {
LocalDate localDate = LocalDate.of(2023, 6, 1);
LocalDate localDate2 = LocalDate.of(2022, 6, 18);
boolean result = localDate.isAfter(localDate2);
System.out.println(result);
assertThat(result).isTrue();
result = localDate2.isAfter(localDate);
System.out.println(result);
assertThat(result).isFalse();
}
true
false
equals
Compare Two LocalDate Instances 建立兩個 LocalDate ,檢查指定的日期是否等於另一個日期。
@Test
public void equals() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2023, 6, 18);
boolean result = localDate.equals(localDate2);
System.out.println(result);
assertThat(result).isTrue();
result = localDate2.equals(localDate);
System.out.println(result);
assertThat(result).isTrue();
}
true
false
isEqual
Compare Two LocalDate Instances 建立兩個 LocalDate ,檢查指定的日期是否等於另一個日期。
@Test
public void isEqual() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2023, 6, 18);
boolean result = localDate.isEqual(localDate2);
System.out.println(result);
assertThat(result).isTrue();
result = localDate2.isEqual(localDate);
System.out.println(result);
assertThat(result).isTrue();
}
true
false
CompareLocalDatesTest.java
Compare Two LocalDate Instances 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.datetime.localdate;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDate;
import org.junit.Test;
public class CompareLocalDatesTest {
@Test
public void compareTo() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
int result = localDate.compareTo(localDate2);
System.out.println(result);
assertThat(result).isLessThan(0);
localDate = LocalDate.of(2023, 6, 18);
localDate2 = LocalDate.of(2023, 6, 18);
result = localDate.compareTo(localDate2);
System.out.println(result);
assertThat(result).isEqualTo(0);
localDate = LocalDate.of(2023, 6, 18);
localDate2 = LocalDate.of(2022, 6, 18);
result = localDate.compareTo(localDate2);
System.out.println(result);
assertThat(result).isGreaterThan(0);
}
@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);
assertThat(result).isTrue();
result = localDate2.isBefore(localDate);
System.out.println(result);
assertThat(result).isFalse();
}
@Test
public void isAfter() {
LocalDate localDate = LocalDate.of(2023, 6, 1);
LocalDate localDate2 = LocalDate.of(2022, 6, 18);
boolean result = localDate.isAfter(localDate2);
System.out.println(result);
assertThat(result).isTrue();
result = localDate2.isAfter(localDate);
System.out.println(result);
assertThat(result).isFalse();
}
@Test
public void equals() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2023, 6, 18);
boolean result = localDate.equals(localDate2);
System.out.println(result);
assertThat(result).isTrue();
result = localDate2.equals(localDate);
System.out.println(result);
assertThat(result).isTrue();
}
@Test
public void isEqual() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2023, 6, 18);
boolean result = localDate.isEqual(localDate2);
System.out.println(result);
assertThat(result).isTrue();
result = localDate2.isEqual(localDate);
System.out.println(result);
assertThat(result).isTrue();
}
}
心得分享
Compare Two LocalDates in Java 比較本地日期,是使用提供的方法來比較並傳回布林值 – true 或 false ,對日期進行運算更加方便,熟悉 Compare Two LocalDate Instances 這些方法的操作,提升開發效率,在應用上相當廣泛。