Core Java

Convert time unit to duration in Java

java.util.concurrent.TimeUnit represents time durations in Java at a given unit of granularity and provides utility methods to convert across units. java.util.concurrent.TimeUnit was introduced back in the old Java days (1.5) but since then it has been extended several times already. In this blog post you will learn how to use java.util.concurrent.TimeUnit to convert a given time unit to a duration .

Prerequisites

You will need JDK 11+ for the examples to work.

Learn how to manage multiple Java SDKs with SDKMAN! with ease

Problem

Convert a given time unit to duration in Java

Solution

Use java.concurrent.TimeUnit enum.

Java 11

  • TimeUnit.convert(Duration duration):
1
2
3
long yearInMillis = TimeUnit.MILLISECONDS.convert(Duration.ofDays(365));
long hourInMillis = TimeUnit.MILLISECONDS.convert(Duration.ofHours(1));
long minuteInMillis = TimeUnit.MILLISECONDS.convert(Duration.ofMinutes(1));
  • TimeUnit.convert(Duration duration) with java.time.ChronoUnit:
1
2
3
long yearInMillis = TimeUnit.MILLISECONDS.convert(Duration.of(365, TimeUnit.DAYS.toChronoUnit()));
long hourInMillis = TimeUnit.MILLISECONDS.convert(Duration.of(1, TimeUnit.HOURS.toChronoUnit()));
long minuteInMillis = TimeUnit.MILLISECONDS.convert(Duration.of(1, TimeUnit.MINUTES.toChronoUnit()));

Pre-Java 11

  • Generic TimeUnit.convert(long sourceDuration, TimeUnit sourceUnit)
1
2
3
long yearInMillis = TimeUnit.DAYS.toMillis(365);
long hourInMillis = TimeUnit.HOURS.toMillis(1);
long minuteInMillis = TimeUnit.MINUTES.toMillis(1);
  • TimeUnit.toMillis(long duration), TimeUnit.toSeconds(long duration) etc. methods:
1
2
3
long yearInMillis = TimeUnit.MILLISECONDS.convert(365, TimeUnit.DAYS);
long hourInMillis = TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
long minuteInMillis = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);

Final example

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Java11TimeUnitConvertTests {
 
    public static final long DAYS_IN_A_YEAR = 365L;
    public static final long HOURS_IN_A_DAY = 24L;
    public static final long MINUTES_IN_AN_HOUR = 60L;
    public static final long SECONDS_IN_A_MINUTE = 60L;
    public static final long MILLISECONDS_IN_A_SECOND = 1000L;
    @Test
    void aYearInMillis() {
 
        // A year in milliseconds
        long yearInMillis = TimeUnit.MILLISECONDS.convert(Duration.ofDays(365)); // since Java 11
 
        assertThat(yearInMillis)
                .isEqualTo(DAYS_IN_A_YEAR * HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE * MILLISECONDS_IN_A_SECOND)
                .isEqualTo(TimeUnit.DAYS.toMillis(365))
                .isEqualTo(TimeUnit.MILLISECONDS.convert(365, TimeUnit.DAYS))
                .isEqualTo(TimeUnit.MILLISECONDS.convert(Duration.of(365, TimeUnit.DAYS.toChronoUnit()))); // since Java 11
 
    }
 
    @Test
    void anHourInMillis() {
 
        long hourInMillis = TimeUnit.MILLISECONDS.convert(Duration.ofHours(1)); // since Java 11
         
        assertThat(hourInMillis)
                .isEqualTo(MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE * MILLISECONDS_IN_A_SECOND)
                .isEqualTo(TimeUnit.HOURS.toMillis(1))
                .isEqualTo(TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS))
                .isEqualTo(TimeUnit.MILLISECONDS.convert(Duration.of(1, TimeUnit.HOURS.toChronoUnit()))); // since Java 11
    }
 
    @Test
    void aMinuteInMillis() {
         
        long minuteInMillis = TimeUnit.MILLISECONDS.convert(Duration.ofMinutes(1)); // since Java 11
         
        assertThat(minuteInMillis)
                .isEqualTo(SECONDS_IN_A_MINUTE * MILLISECONDS_IN_A_SECOND)
                .isEqualTo(TimeUnit.MINUTES.toMillis(1))
                .isEqualTo(TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES))
                .isEqualTo(TimeUnit.MILLISECONDS.convert(Duration.of(1, TimeUnit.MINUTES.toChronoUnit()))); // since Java 11
    }
 
    @ParameterizedTest
    @MethodSource("timeUnitToDuration")
    void convertTimeUnitToDuration(TimeUnit unit, Duration duration, long expectedResult) {
        long actualResult = unit.convert(duration);
 
        assertThat(actualResult).isEqualTo(expectedResult);
    }
 
    private static Stream<Arguments> timeUnitToDuration() {
        return Stream.of(
                Arguments.of(TimeUnit.DAYS, Duration.ofHours(24), 1), // 24h = 1d
                Arguments.of(TimeUnit.DAYS, Duration.ofHours(40), 1), // 40h = 1d
                Arguments.of(TimeUnit.DAYS, Duration.ofHours(48), 2), // 48h = 2d
                Arguments.of(TimeUnit.DAYS, Duration.ofHours(50), 2),  // 50h = 2d
                Arguments.of(TimeUnit.MILLISECONDS, Duration.ofSeconds(1), 1000),  // 1s = 1000ms
                Arguments.of(TimeUnit.MILLISECONDS, Duration.ofSeconds(11), 11000),  // 1s = 11000ms
                Arguments.of(TimeUnit.SECONDS, Duration.ofHours(1), 3600),  // 1h = 3600s
                Arguments.of(TimeUnit.HOURS, Duration.ofSeconds(4_000), 1),  // 4000s = 1h
                Arguments.of(TimeUnit.HOURS, Duration.ofSeconds(1_0000), 2// 10000s = 2h
        );
    }
}

Source code

The source code for this article can be found on Github: https://github.com/kolorobot/java9-and-beyond

Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: Convert time unit to duration in Java

Opinions expressed by Java Code Geeks contributors are their own.

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button