6
我想有一个看起来像转换“时间”,“单位”,以毫秒为单位
public static long toMillis (long duration, ChronoUnit unit) {
// magic duration to millis
}
toMillis(5, ChronoUnit.SECONDS); // 5000
当然
我没有实际编写toMillis
的API,但是这基本上就是我想要做的事。
您可以用['ChronoUnit#getDuration()'](https://docs.oracle.com/javase/8/docs/api/java/ time/temporal/ChronoUnit.html#getDuration--),然后使用['Duration#toMillis()']将其转换为毫秒(https://docs.oracle.com/javase/8/docs/api/java/time /Duration.html#toMillis--)? – Whymarrh
我认为这与日期/时间API有关。如果你只是在寻找一个单位转换工具,你可以使用'TimeUnit.SECONDS.toMillis(duration)'。 – shmosel
@shmosel奇怪的是,我在这里发现了API,它可以达到我的目的,但后来我对“为什么我不知道如何使用java.time做到这一点”感到沮丧。 – xenoterracide