java.util.concurrent.TimeUnit
java.util.concurrent.TimeUnit
早在以前的Java时代(1.5)就被引入,但从那时起,它已经被扩展了好几次。在这篇博文中,你将学习如何使用 ,将一个给定的java.util.concurrent.TimeUnit
时间单位转换成一个持续时间。
前提条件
你需要JDK 11+才能使用这些例子:
了解如何使用SDKMAN管理多个Java SDK!轻松搞定
问题
在Java中把给定的时间单位转换为期限
解决方法
使用java.concurrent.TimeUnit
枚举。
Java 11
TimeUnit.convert(Duration duration)
:
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)
使用 。java.time.ChronoUnit
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()));
复制代码
前Java 11
- 通用的
TimeUnit.convert(long sourceDuration, TimeUnit sourceUnit)
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)
等方法。
long yearInMillis = TimeUnit.MILLISECONDS.convert(365, TimeUnit.DAYS);
long hourInMillis = TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
long minuteInMillis = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
复制代码
最后的例子
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
);
}
}
复制代码
源代码
本文的源代码可以在Github上找到:https://github.com/kolorobot/java9-and-beyond