1. Overview
1.概述
Duration is an amount of time expressed in terms of hours, minutes, seconds, milliseconds, and so on. We may wish to format a duration into some particular time pattern.
持续时间是以小时、分钟、秒、毫秒等表示的时间量。我们可能希望将一个持续时间格式化为某种特定的时间模式。
We can achieve this either by writing custom code with the help of some JDK libraries or by making use of third-party libraries.
我们可以通过在一些JDK库的帮助下编写自定义代码或利用第三方库来实现这一点。
In this quick tutorial, we’ll look at how to write simple code to format a given duration to HH:MM:SS format.
在这个快速教程中,我们将看看如何编写简单的代码,将一个给定的持续时间格式化为HH:MM:SS格式。
2. Java Solutions
2.Java解决方案
There are multiple ways a duration can be expressed — for example, in minutes, seconds, and milliseconds, or as a Java Duration, which has its own specific format.
持续时间有多种表达方式–例如,以分、秒和毫秒为单位,或者以Java Duration为单位,它有自己的特定格式。
This section and subsequent sections will focus on formatting intervals (elapsed time), specified in milliseconds, to HH:MM:SS using some JDK libraries. For the sake of our examples, we’ll be formatting 38114000ms as 10:35:14 (HH:MM:SS).
本节和后续章节将重点讨论使用一些JDK库将以毫秒为单位的时间间隔(经过的时间)格式化为HH:MM:SS。在我们的例子中,我们将把38114000ms格式化为10:35:14(HH:MM:SS)。
2.1. Duration
2.1 期限
As of Java 8, the Duration class was introduced to handle intervals of time in various units. The Duration class comes with a lot of helper methods to get the hours, minutes, and seconds from a duration.
从Java 8开始,引入了Duration类,以处理各种单位的时间间隔。Duration类带有许多辅助方法,以从一个持续时间中获得小时、分钟和秒。
To format an interval to HH:MM:SS using the Duration class, we need to initialize the Duration object from our interval using the factory method ofMillis found in the Duration class. This converts the interval to a Duration object that we can work with:
要使用Duration类将一个时间间隔格式化为HH:MM:SS,我们需要使用Duration类中的工厂方法ofMillis来初始化我们的时间间隔的Duration对象。这可以将时间间隔转换为我们可以使用的Duration对象。
Duration duration = Duration.ofMillis(38114000);
For ease of calculation from seconds to our desired units, we need to get the total number of seconds in our duration or interval:
为了便于从秒到我们所需单位的计算,我们需要得到我们的持续时间或间隔时间的总秒数。
long seconds = duration.getSeconds();
Then, once we have the number of seconds, we generate the corresponding hours, minutes, and seconds for our desired format:
然后,一旦我们得到秒数,我们就为我们所需的格式生成相应的小时、分钟和秒。
long HH = seconds / 3600;
long MM = (seconds % 3600) / 60;
long SS = seconds % 60;
Finally, we format our generated values:
最后,我们对生成的数值进行格式化。
String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS);
Let’s try this solution out:
让我们来试试这个解决方案。
assertThat(timeInHHMMSS).isEqualTo("10:35:14");
If we’re using Java 9 or later, we can use some helper methods to get the units directly without having to perform any calculations:
如果我们使用的是Java 9或更高版本,我们可以使用一些辅助方法来直接获得单位,而不需要进行任何计算。
long HH = duration.toHours();
long MM = duration.toMinutesPart();
long SS = duration.toSecondsPart();
String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS);
The above snippet will give us the same result as tested above:
上述片段将给我们带来与上述测试相同的结果。
assertThat(timeInHHMMSS).isEqualTo("10:35:14");
2.2. TimeUnit
2.2.时间单位
Just like the Duration class discussed in the previous section, TimeUnit represents a time at a given granularity. It provides some helper methods to convert across units – which in our case would be hours, minutes, and seconds – and to perform timing and delay operations in these units.
就像上一节中讨论的Duration类一样,TimeUnit表示一个特定粒度的时间。它提供了一些辅助方法来转换不同的单位(在我们的例子中是小时、分钟和秒),并在这些单位中执行计时和延迟操作。
To format a duration in milliseconds to the format HH:MM:SS, all we need to do is to use the corresponding helper methods in TimeUnit:
要将持续时间以毫秒为单位格式化为HH:MM:SS,我们所要做的就是使用TimeUnit中的相应辅助方法。
long HH = TimeUnit.MILLISECONDS.toHours(38114000);
long MM = TimeUnit.MILLISECONDS.toMinutes(38114000) % 60;
long SS = TimeUnit.MILLISECONDS.toSeconds(38114000) % 60;
Then, format the duration based on generated units above:
然后,根据上面生成的单位格式化持续时间。
String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS);
assertThat(timeInHHMMSS).isEqualTo("10:35:14");
3. Using Third-Party Libraries
3.使用第三方库
We may choose to try a different route by using third-party library methods rather than writing our own.
我们可以选择使用第三方库的方法,而不是自己编写,来尝试不同的路线。
3.1. Apache Commons
3.1.Apache Commons
To use Apache Commons, we need to add commons-lang3 to our project:
要使用Apache Commons,我们需要在我们的项目中添加commons-lang3。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
As expected, this library has formatDuration as well as other unit formatting methods in its DurationFormatUtils class:
正如预期的那样,这个库在其DurationFormatUtils类中有formatDuration以及其他单位格式化方法。
String timeInHHMMSS = DurationFormatUtils.formatDuration(38114000, "HH:MM:SS", true);
assertThat(timeInHHMMSS).isEqualTo("10:35:14");
3.2. Joda Time
3.2 乔达时间
The Joda Time library comes in handy when we’re using a Java version prior to Java 8 because of its handy helper methods to represent and format units of time. To use Joda Time, let’s add the joda-time dependency to our project:
当我们使用 Java 8 之前的 Java 版本时,Joda Time库就会派上用场,因为它有方便的辅助方法来表示和格式化时间单位。为了使用Joda Time,让我们将joda-time依赖项添加到我们的项目中。
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
Joda Time has a Duration class to represent time. First, we convert the interval in milliseconds to an instance of the Joda Time Duration object:
Joda Time有一个Duration类来表示时间。首先,我们把以毫秒为单位的时间间隔转换成Joda Time Duration对象的实例。
Duration duration = new Duration(38114000);
Then, we get the period from the duration above using the toPeriod method in Duration, which converts or initializes it to an instance of the Period class in Joda Time:
然后,我们使用Duration中的toPeriod方法从上面的持续时间中获得周期,该方法将其转换或初始化为Joda Time中Period类的一个实例。
Period period = duration.toPeriod();
We get the units (hours, minutes, and seconds) from Period using its corresponding helper methods:
我们使用Period的相应帮助方法从Period获得单位(小时、分钟和秒)。
long HH = period.getHours();
long MM = period.getMinutes();
long SS = period.getSeconds();
Finally, we can format the duration and test the result:
最后,我们可以格式化持续时间并测试结果。
String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS);
assertThat(timeInHHMMSS).isEqualTo("10:35:14");
4. Conclusion
4.总结
In this tutorial, we’ve learned how to format a duration to a specific format (HH:MM:SS, in our case).
在本教程中,我们已经学会了如何将一个持续时间格式化为一个特定的格式(在我们的例子中是HH:MM:SS)。
First, we used Duration and TimeUnit classes that come with Java to get the required units and format them with the help of Formatter.
首先,我们使用Java自带的Duration和TimeUnit类来获得所需的单位,并在Formatter的帮助下将其格式化。
Finally, we looked at how to use some third-party libraries to achieve the result.
最后,我们研究了如何使用一些第三方库来实现这一结果。
As usual, the complete source code is available over on GitHub.
像往常一样,完整的源代码可以在GitHub上找到,。