Table of Contents
ToggleDifference Between Two LocalTime in Java
計算兩個時間差距,提供了幾種方法找到不同時間單位,取得兩者之前的差異,提供了一些常見的方法,例如: Duration 、 ChronoUnit 、 until 等方式, Calculate Difference Between Two LocalTime 可以計算兩個時間之間的差異,簡化了操作時間比對的方法 ,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- localtime
| +- DifferenceTwoLocalTimeTest.java
單元測試
Java Time Between Two LocalTime 計算兩個 LocalTime 物件的差異。
Duration_between
Java Time Between Two LocalTime 建立兩個 LocalTime ,使用 Duration between 靜態方法,可以採用不同的時間單位,計算兩者之間的差異。
@Test
public void Duration_between() {
LocalTime localTime = LocalTime.of(9, 12, 5, 123456789);
LocalTime localTime2 = LocalTime.of(10, 12, 5, 123456789);
Duration duration = Duration.between(localTime, localTime2);
System.out.println(duration);
long hours = duration.toHours();
System.out.println(hours);
assertThat(hours).isEqualTo(1);
long minutes = duration.toMinutes();
System.out.println(minutes);
assertThat(minutes).isEqualTo(60);
long seconds = duration.getSeconds();
System.out.println(seconds);
assertThat(seconds).isEqualTo(3600);
long nanos = duration.toNanos();
System.out.println(nanos);
assertThat(nanos).isEqualTo(3600000000000L);
}
PT1H
1
60
3600
3600000000000
ChronoUnit_between
Java Time Between Two LocalTime 建立兩個 LocalTime ,使用 ChronoUnit Enum ,可以採用不同的時間單位,計算兩者之間的差異。
@Test
public void ChronoUnit_between() {
LocalTime localTime = LocalTime.of(9, 12, 5, 123456789);
LocalTime localTime2 = LocalTime.of(10, 12, 5, 123456789);
long hours = ChronoUnit.HOURS.between(localTime, localTime2);
System.out.println(hours);
assertThat(hours).isEqualTo(1);
long minutes = ChronoUnit.MINUTES.between(localTime, localTime2);
System.out.println(minutes);
assertThat(minutes).isEqualTo(60);
long seconds = ChronoUnit.SECONDS.between(localTime, localTime2);
System.out.println(seconds);
assertThat(seconds).isEqualTo(3600);
long milliseconds = ChronoUnit.MILLIS.between(localTime, localTime2);
System.out.println(milliseconds);
assertThat(milliseconds).isEqualTo(3600000);
long nano = ChronoUnit.NANOS.between(localTime, localTime2);
System.out.println(nano);
assertThat(nano).isEqualTo(3600000000000L);
}
1
60
3600
3600000
3600000000000
until
Java Time Between Two LocalTime 建立兩個 LocalTime ,可以採用不同的時間單位,計算兩者之間的差異, Java LocalTime Difference Between Two Dates 提供範例參考。
@Test
public void until() {
LocalTime localTime = LocalTime.of(9, 12, 5, 123456789);
LocalTime localTime2 = LocalTime.of(10, 12, 5, 987654321);
long hours = localTime.until(localTime2, ChronoUnit.HOURS);
System.out.println(hours);
assertThat(hours).isEqualTo(1);
long minutes = localTime.until(localTime2, ChronoUnit.MINUTES);
System.out.println(minutes);
assertThat(minutes).isEqualTo(60);
long seconds = localTime.until(localTime2, ChronoUnit.SECONDS);
System.out.println(seconds);
assertThat(seconds).isEqualTo(3600);
long milliseconds = localTime.until(localTime2, ChronoUnit.MILLIS);
System.out.println(milliseconds);
assertThat(milliseconds).isEqualTo(3600864);
long nano = localTime.until(localTime2, ChronoUnit.NANOS);
System.out.println(nano);
assertThat(nano).isEqualTo(3600864197532L);
}
1
60
3600
3600864
3600864197532
DifferenceTwoLocalTimeTest.java
Difference Between Two LocalTime in Multiple Units 新增單元測試,驗證 Java LocalTime Difference Between Two Dates 是否符合預期。
package org.ruoxue.java_147.datetime.localtime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
public class DifferenceTwoLocalTimeTest {
@Test
public void Duration_between() {
LocalTime localTime = LocalTime.of(9, 12, 5, 123456789);
LocalTime localTime2 = LocalTime.of(10, 12, 5, 123456789);
Duration duration = Duration.between(localTime, localTime2);
System.out.println(duration);
long hours = duration.toHours();
System.out.println(hours);
assertThat(hours).isEqualTo(1);
long minutes = duration.toMinutes();
System.out.println(minutes);
assertThat(minutes).isEqualTo(60);
long seconds = duration.getSeconds();
System.out.println(seconds);
assertThat(seconds).isEqualTo(3600);
long nanos = duration.toNanos();
System.out.println(nanos);
assertThat(nanos).isEqualTo(3600000000000L);
}
@Test
public void ChronoUnit_between() {
LocalTime localTime = LocalTime.of(9, 12, 5, 123456789);
LocalTime localTime2 = LocalTime.of(10, 12, 5, 123456789);
long hours = ChronoUnit.HOURS.between(localTime, localTime2);
System.out.println(hours);
assertThat(hours).isEqualTo(1);
long minutes = ChronoUnit.MINUTES.between(localTime, localTime2);
System.out.println(minutes);
assertThat(minutes).isEqualTo(60);
long seconds = ChronoUnit.SECONDS.between(localTime, localTime2);
System.out.println(seconds);
assertThat(seconds).isEqualTo(3600);
long milliseconds = ChronoUnit.MILLIS.between(localTime, localTime2);
System.out.println(milliseconds);
assertThat(milliseconds).isEqualTo(3600000);
long nano = ChronoUnit.NANOS.between(localTime, localTime2);
System.out.println(nano);
assertThat(nano).isEqualTo(3600000000000L);
}
@Test
public void until() {
LocalTime localTime = LocalTime.of(9, 12, 5, 123456789);
LocalTime localTime2 = LocalTime.of(10, 12, 5, 987654321);
long hours = localTime.until(localTime2, ChronoUnit.HOURS);
System.out.println(hours);
assertThat(hours).isEqualTo(1);
long minutes = localTime.until(localTime2, ChronoUnit.MINUTES);
System.out.println(minutes);
assertThat(minutes).isEqualTo(60);
long seconds = localTime.until(localTime2, ChronoUnit.SECONDS);
System.out.println(seconds);
assertThat(seconds).isEqualTo(3600);
long milliseconds = localTime.until(localTime2, ChronoUnit.MILLIS);
System.out.println(milliseconds);
assertThat(milliseconds).isEqualTo(3600864);
long nano = localTime.until(localTime2, ChronoUnit.NANOS);
System.out.println(nano);
assertThat(nano).isEqualTo(3600864197532L);
}
}
心得分享
Java LocalTime Difference Between Two Dates 計算時間,提供不同的時間單位,取得其差異,對時間進行運算更加方便,熟悉 Difference Between Two LocalTime in Multiple Units 這些方法的操作,提升開發效率,在應用上相當廣泛。