From 622ac3e401b4e9850a359b22360918102a4eb867 Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Mon, 30 Aug 2021 19:41:25 -0500 Subject: [PATCH] BAEL-5064 Format Duration (#11144) * BAEL-5064 Format Duration * BAEL-5013 deleted utility class * BAEL-5013 format duration * changed Joda-time version Co-authored-by: Remy Ohajinwa --- .../core-java-datetime-string/pom.xml | 2 +- .../FormatDurationUnitTest.java | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-datetime-string/src/test/java/com/baeldung/formatduration/FormatDurationUnitTest.java diff --git a/core-java-modules/core-java-datetime-string/pom.xml b/core-java-modules/core-java-datetime-string/pom.xml index dc0c5fd8b1..f50eb2ae5e 100644 --- a/core-java-modules/core-java-datetime-string/pom.xml +++ b/core-java-modules/core-java-datetime-string/pom.xml @@ -75,7 +75,7 @@ 1.6 - 2.10 + 2.10.10 3.6.1 1.9 diff --git a/core-java-modules/core-java-datetime-string/src/test/java/com/baeldung/formatduration/FormatDurationUnitTest.java b/core-java-modules/core-java-datetime-string/src/test/java/com/baeldung/formatduration/FormatDurationUnitTest.java new file mode 100644 index 0000000000..1612edd30b --- /dev/null +++ b/core-java-modules/core-java-datetime-string/src/test/java/com/baeldung/formatduration/FormatDurationUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.formatduration; + +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.joda.time.Period; +import org.junit.Test; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FormatDurationUnitTest { + + + @Test + public void givenInterval_WhenFormatInterval_formatDuration() { + long HH = TimeUnit.MILLISECONDS.toHours(38114000); + long MM = TimeUnit.MILLISECONDS.toMinutes(38114000) % 60; + long SS = TimeUnit.MILLISECONDS.toSeconds(38114000) % 60; + String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS); + + assertThat(timeInHHMMSS).isEqualTo("10:35:14"); + } + + @Test + public void givenInterval_WhenFormatUsingDuration_formatDuration() { + Duration duration = Duration.ofMillis(38114000); + long seconds = duration.getSeconds(); + long HH = seconds / 3600; + long MM = (seconds % 3600) / 60; + long SS = seconds % 60; + String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS); + assertThat(timeInHHMMSS).isEqualTo("10:35:14"); + } + + + @Test + public void givenInterval_WhenFormatDurationUsingApacheCommons_formatDuration() { + assertThat(DurationFormatUtils.formatDuration(38114000, "HH:mm:ss")) + .isEqualTo("10:35:14"); + } + + @Test + public void givenInterval_WhenFormatDurationUsingJodaTime_formatDuration() { + org.joda.time.Duration duration = new org.joda.time.Duration(38114000); + Period period = duration.toPeriod(); + long HH = period.getHours(); + long MM = period.getMinutes(); + long SS = period.getSeconds(); + + String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS); + assertThat(timeInHHMMSS).isEqualTo("10:35:14"); + } +}