在 Java 中将持续时间字符串转换为毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15410512/
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-10-31 19:29:52  来源:igfitidea点击:

Converting duration string into milliseconds in Java

javaduration

提问by Sunny

I apologize if this has already been discussed.

如果已经讨论过这个问题,我深表歉意。

I have a clip duration in a string:

我在一个字符串中有一个剪辑持续时间:

00:10:17

I would like to convert that to value in milliseconds. (Basically 617000 for the above string)

我想将其转换为以毫秒为单位的值。(对于上述字符串,基本上是 617000)

Is there some API that I could use to do this in one or two steps. On basic way would be to split the string and then add the minutes, seconds and hours.

是否有一些 API 可以用来在一两个步骤中完成此操作。基本方法是拆分字符串,然后添加分钟、秒和小时。

But is there a shorter way to do this?

但是有没有更短的方法来做到这一点?

Thanks.

谢谢。

回答by assylias

You could simply parse it manually:

您可以简单地手动解析它:

String s = "00:10:17";
String[] data = s.split(":");

int hours  = Integer.parseInt(data[0]);
int minutes = Integer.parseInt(data[1]);
int seconds = Integer.parseInt(data[2]);

int time = seconds + 60 * minutes + 3600 * hours;
System.out.println("time in millis = " + TimeUnit.MILLISECONDS.convert(time, TimeUnit.SECONDS));

回答by Simon Dorociak

Here is one own written possible approach(assumption of same source format always)

这是一种自己编写的可能方法(始终假设相同的源格式)

String source = "00:10:17";
String[] tokens = source.split(":");
int secondsToMs = Integer.parseInt(tokens[2]) * 1000;
int minutesToMs = Integer.parseInt(tokens[1]) * 60000;
int hoursToMs = Integer.parseInt(tokens[0]) * 3600000;
long total = secondsToMs + minutesToMs + hoursToMs;

回答by Basil Bourque

java.time.Duration

java.time.Duration

Use the Durationclass.

使用Duration类。

Duration.between( 
    LocalTime.MIN , 
    LocalTime.parse( "00:10:17" ) 
).toMillis()

milliseconds: 617000

毫秒:617000

See this code live in IdeOne.com.

在 IdeOne.com 中查看此代码

See this Answer of mineand this Answer of mineto similar Questions for more explanation of Duration, LocalTime, and a java.time.

我这个答案这个答案我的类似问题进行更多的解释DurationLocalTime以及java.time。

Caution:Beware of possible data-loss when converting from possible values of nanosecondsin java.time objects to your desired milliseconds.

注意:当从java.time 对象中可能的纳秒值转换为所需的毫秒值时,请注意可能的数据丢失。

回答by NilsH

Check out the Joda TimeDurationclass.

查看Joda Time Duration类。

回答by Bhavesh Shah

Create an appropriate instance of DateFormat. Use the parse()method to get a Date. Use getTime()on the Dateobject to get milliseconds.

创建一个合适的DateFormat. 使用该parse()方法获得一个Date. getTime()Date对象上使用以获取毫秒。

You can check here.

你可以在这里查看

I just tested it as:

我只是将其测试为:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Test {        
    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        String inputString = "00:10:17";

        Date date = sdf .parse(inputString);// .parse("1970-01-01" + inputString);
        System.out.println("in milliseconds: " + date.getTime());        
    }
}