如何将 HH:mm:ss.SSS 转换为毫秒?
我有一个字符串 00:01:30.500
相当于 90500
毫秒。我尝试使用 SimpleDateFormat
给出包括当前日期在内的毫秒数。我只需要毫秒级的字符串表示形式。我是否必须编写自定义方法来分割并计算毫秒?或者还有其他方法可以做到这一点吗?谢谢。
我尝试过如下:
String startAfter = "00:01:30.555";
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = dateFormat.parse(startAfter);
System.out.println(date.getTime());
I have a String 00:01:30.500
which is equivalent to 90500
milliseconds. I tried using SimpleDateFormat
which give milliseconds including current date. I just need that String representation to milliseconds. Do I have to write custom method, which will split and calculate milliseconds? or Is there any other way to do this? Thanks.
I have tried as follows:
String startAfter = "00:01:30.555";
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = dateFormat.parse(startAfter);
System.out.println(date.getTime());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
SimpleDateFormat
来完成此操作。你只需要知道两件事。.getTime()
返回自 1970-01-01 00:00:00 UTC 以来的毫秒数。You can use
SimpleDateFormat
to do it. You just have to know 2 things..getTime()
returns the number of milliseconds since 1970-01-01 00:00:00 UTC.如果您想自己解析格式,您可以使用正则表达式轻松完成,例如
但是,这种解析非常宽松,会接受 99:99:99.999 并让值溢出。这可能是一个缺点,也可能是一个特点。
If you want to parse the format yourself you could do it easily with a regex such as
However, this parsing is quite lenient and would accept 99:99:99.999 and just let the values overflow. This could be a drawback or a feature.
使用 JODA:
Using JODA:
如果您想使用
SimpleDateFormat
,您可以这样写:但是自定义方法会更有效。
SimpleDateFormat
由于其所有日历支持、时区支持、夏令时支持等,速度相当慢。如果您确实需要其中一些功能,那么缓慢是值得的,但既然您不需要,则可能不需要。 (这取决于您调用此方法的频率,以及您的应用程序是否需要考虑效率。)此外,
SimpleDateFormat
是非线程安全的,这有时会很痛苦。 (在不了解您的应用程序的情况下,我无法猜测这是否重要。)就我个人而言,我可能会编写一个自定义方法。
If you want to use
SimpleDateFormat
, you could write:But a custom method would be much more efficient.
SimpleDateFormat
, because of all its calendar support, time-zone support, daylight-savings-time support, and so on, is pretty slow. The slowness is worth it if you actually need some of those features, but since you don't, it might not be. (It depends how often you're calling this method, and whether efficiency is a concern for your application.)Also,
SimpleDateFormat
is non-thread-safe, which is sometimes a pain. (Without knowing anything about your application, I can't guess whether that matters.)Personally, I'd probably write a custom method.
我提供两个选项:
SimpleDateFormat
和Date
是错误的类,因为 1 分钟 30.5 秒的持续时间不是日期,而且这些类早已不再合理使用。Time4J
这是一个优雅的解决方案。我们首先声明一个格式化程序:
然后解析并转换为毫秒,如下所示:
输出为:
java.time
java.time.Duration
类只能解析 ISO 8601 格式。所以我首先将您的字符串转换为该格式。它就像PT00H01M30.555S
(不需要前导零,但我为什么要费心删除它们?)输出与以前相同:
与 Time4J 的另一个区别是 Java
Duration
可以直接转换为毫秒,而无需先转换为只有毫秒的Duration
。链接
I am presenting two options:
SimpleDateFormat
andDate
are the wrong classes to use, both because a duration of 1 minute 30.5 seoncds is not a date and because those classes have long gone out of any reasonable use.Time4J
This is the elegant solution. We first declare a formatter:
Then parse and convert to milliseconds like this:
Output is:
java.time
The
java.time.Duration
class can only parse ISO 8601 format. So I am first converting your string to that format. It goes likePT00H01M30.555S
(the leading zeroes are not required, but why should I bother removing them?)Output is the same as before:
Another difference from Time4J is that the Java
Duration
can be directly converted to milliseconds without being converted to aDuration
of only milliseconds first.Links
对于 .NET / C# 如果 timeString = "00:00:00:014"
假设我们有一个 GetNumeric() 函数来优雅地将 String 处理为 Int。
For .NET / C# if timeString = "00:00:00:014"
Assume we have a GetNumeric() function to handle the String to Int gracefully.