java 将“持续时间”“单位”转换为毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37714146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:42:24  来源:igfitidea点击:

Convert "duration" "Unit" to milliseconds

javajava-time

提问by xenoterracide

I want to have an API that looks like

我想要一个看起来像的 API

public static long toMillis ( long duration, ChronoUnit unit ) {
   // magic duration to millis
}

toMillis( 5, ChronoUnit.SECONDS); // 5000

of course I'm not actually writing toMillisbut that's essentially what I'm trying to do.

当然,我实际上并不是在写作,toMillis但这基本上就是我想要做的。

回答by Andrew Rueckert

You can use Durationfor this:

您可以Duration为此使用:

Duration.of(5, ChronoUnit.SECONDS).toMillis()