Table of Contents
ToggleDifference Between Two LocalDate in Java
計算兩個日期差距,提供了幾種方法找到不同時間單位,取得兩者之前的差異,提供了一些常見的方法,例如: Period 、Duration 、 ChronoUnit 、 until 等方式, Calculate Difference Between Two LocalDate 可以計算兩個日期之間的差異,簡化了操作日期比對的方法 ,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localdate
| +- DifferenceTwoLocalDateTest.java
單元測試
Java Time Between Two LocalDate 計算兩個 LocalDate 物件的差異。
Period_between
Java Time Between Two LocalDate 建立兩個 LocalDate ,使用 Period between 靜態方法,可以採用不同的時間單位,計算兩者之間的差異。
@Test
public void Period_between() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
Period period = Period.between(localDate, localDate2);
System.out.println(period);
long years = period.getYears();
System.out.println(years);
assertThat(years).isEqualTo(1);
long months = period.getMonths();
System.out.println(months);
assertThat(months).isEqualTo(0);
long days = period.getDays();
System.out.println(days);
assertThat(days).isEqualTo(0);
}
P1Y
1
0
0
Duration_between
Java Time Between Two LocalDate 建立兩個 LocalDate ,使用 Duration between 靜態方法,可以採用不同的時間單位,計算兩者之間的差異。
@Test
public void Duration_between() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
Duration duration = Duration.between(localDate.atStartOfDay(), localDate2.atStartOfDay());
System.out.println(duration);
long days = duration.toDays();
System.out.println(days);
assertThat(days).isEqualTo(366);
long hours = duration.toHours();
System.out.println(hours);
assertThat(hours).isEqualTo(8784);
long minutes = duration.toMinutes();
System.out.println(minutes);
assertThat(minutes).isEqualTo(527040);
long seconds = duration.getSeconds();
System.out.println(seconds);
assertThat(seconds).isEqualTo(31622400);
long nanos = duration.toNanos();
System.out.println(nanos);
assertThat(nanos).isEqualTo(31622400000000000L);
}
PT8784H
366
8784
527040
31622400
31622400000000000
ChronoUnit_between
Java Time Between Two LocalDate 建立兩個 LocalDate ,使用 ChronoUnit Enum ,可以採用不同的時間單位,計算兩者之間的差異。
@Test
public void ChronoUnit_between() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
long years = ChronoUnit.YEARS.between(localDate, localDate2);
System.out.println(years);
assertThat(years).isEqualTo(1);
long months = ChronoUnit.MONTHS.between(localDate, localDate2);
System.out.println(months);
assertThat(months).isEqualTo(12);
long weeks = ChronoUnit.WEEKS.between(localDate, localDate2);
System.out.println(weeks);
assertThat(weeks).isEqualTo(52);
long days = ChronoUnit.DAYS.between(localDate, localDate2);
System.out.println(days);
assertThat(days).isEqualTo(366);
}
1
12
52
366
until
Java Time Between Two LocalDate 建立兩個 LocalDate ,可以採用不同的時間單位,計算兩者之間的差異, Java LocalDate Difference Between Two Dates 提供範例參考。
@Test
public void until() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
long years = localDate.until(localDate2, ChronoUnit.YEARS);
System.out.println(years);
assertThat(years).isEqualTo(1);
long months = localDate.until(localDate2, ChronoUnit.MONTHS);
System.out.println(months);
assertThat(months).isEqualTo(12);
long weeks = localDate.until(localDate2, ChronoUnit.WEEKS);
System.out.println(weeks);
assertThat(weeks).isEqualTo(52);
long days = localDate.until(localDate2, ChronoUnit.DAYS);
System.out.println(days);
assertThat(days).isEqualTo(366);
}
1
12
52
366
DifferenceTwoLocalDateTest.java
Difference Between Two LocalDate in Multiple Units 新增單元測試,驗證 Java LocalDate Difference Between Two Dates 是否符合預期。
package org.ruoxue.java_147.datetime.localdate;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
public class DifferenceTwoLocalDateTest {
@Test
public void Period_between() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
Period period = Period.between(localDate, localDate2);
System.out.println(period);
long years = period.getYears();
System.out.println(years);
assertThat(years).isEqualTo(1);
long months = period.getMonths();
System.out.println(months);
assertThat(months).isEqualTo(0);
long days = period.getDays();
System.out.println(days);
assertThat(days).isEqualTo(0);
}
@Test
public void Duration_between() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
Duration duration = Duration.between(localDate.atStartOfDay(), localDate2.atStartOfDay());
System.out.println(duration);
long days = duration.toDays();
System.out.println(days);
assertThat(days).isEqualTo(366);
long hours = duration.toHours();
System.out.println(hours);
assertThat(hours).isEqualTo(8784);
long minutes = duration.toMinutes();
System.out.println(minutes);
assertThat(minutes).isEqualTo(527040);
long seconds = duration.getSeconds();
System.out.println(seconds);
assertThat(seconds).isEqualTo(31622400);
long nanos = duration.toNanos();
System.out.println(nanos);
assertThat(nanos).isEqualTo(31622400000000000L);
}
@Test
public void ChronoUnit_between() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
long years = ChronoUnit.YEARS.between(localDate, localDate2);
System.out.println(years);
assertThat(years).isEqualTo(1);
long months = ChronoUnit.MONTHS.between(localDate, localDate2);
System.out.println(months);
assertThat(months).isEqualTo(12);
long weeks = ChronoUnit.WEEKS.between(localDate, localDate2);
System.out.println(weeks);
assertThat(weeks).isEqualTo(52);
long days = ChronoUnit.DAYS.between(localDate, localDate2);
System.out.println(days);
assertThat(days).isEqualTo(366);
}
@Test
public void until() {
LocalDate localDate = LocalDate.of(2023, 6, 18);
LocalDate localDate2 = LocalDate.of(2024, 6, 18);
long years = localDate.until(localDate2, ChronoUnit.YEARS);
System.out.println(years);
assertThat(years).isEqualTo(1);
long months = localDate.until(localDate2, ChronoUnit.MONTHS);
System.out.println(months);
assertThat(months).isEqualTo(12);
long weeks = localDate.until(localDate2, ChronoUnit.WEEKS);
System.out.println(weeks);
assertThat(weeks).isEqualTo(52);
long days = localDate.until(localDate2, ChronoUnit.DAYS);
System.out.println(days);
assertThat(days).isEqualTo(366);
}
}
心得分享
Java LocalDate Difference Between Two Dates 計算日期,提供不同的時間單位,取得其差異,對日期進行運算更加方便,熟悉 Difference Between Two LocalDate in Multiple Units 這些方法的操作,提升開發效率,在應用上相當廣泛。