如何将毫秒转换为“hh:mm:ss”格式?
我很困惑。在偶然发现这个线程之后,我试图弄清楚如何格式化具有 hh:mm:ss
格式的倒计时器。
这是我的尝试 -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
因此,当我尝试像 3600000ms
这样的值时,我得到 01:59:00
,这是错误的,因为它应该是 01:00: 00
。显然我的逻辑有问题,但目前我看不出问题是什么!
有人可以帮忙吗?
编辑-
修复它。以下是将毫秒格式化为 hh:mm:ss
格式的正确方法 -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
问题在于 TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
。它应该是这个 TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
。
I'm confused. After stumbling upon this thread, I tried to figure out how to format a countdown timer that had the format hh:mm:ss
.
Here's my attempt -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
So, when I try a value like 3600000ms
, I get 01:59:00
, which is wrong since it should be 01:00:00
. Obviously there's something wrong with my logic, but at the moment, I cannot see what it is!
Can anyone help?
Edit -
Fixed it. Here's the right way to format milliseconds to hh:mm:ss
format -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
The problem was this TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
. It should have been this TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
您真的很接近:
您使用分钟而不是小时将小时转换为毫秒。
顺便说一句,我喜欢您使用
TimeUnit
API :)这是一些测试代码:
输出:
我意识到通过使用模数除法而不是减法可以大大简化上面的代码:
仍然使用
TimeUnit
API 适用于所有魔法值,并给出完全相同的输出。You were really close:
You were converting hours to millisseconds using minutes instead of hours.
BTW, I like your use of the
TimeUnit
API :)Here's some test code:
Output:
I realised that my code above can be greatly simplified by using a modulus division instead of subtraction:
Still using the
TimeUnit
API for all magic values, and gives exactly the same output.其通用方法相当简单:
The generic method for this is fairly simple:
如果您使用的是 apache commons:
If you are using apache commons:
我用过这个:
请参阅类 Formatter 的描述。
请参阅使用 2400 毫秒输入的可运行示例。
I used this:
See description of class Formatter.
See runnable example using input of 2400 ms.
您还可以使用新的 DateTime API
You can also use new DateTime API
这对我有用,与 kotlin 一起
this worked for me, with kotlin
Java 9
打印:
你让库方法为你完成涉及的转换是正确的。
java.time
,现代 Java 日期和时间 API,或者更准确地说,它的Duration
类比 TimeUnit 更优雅、更不易出错。 。我使用的toMinutesPart和toSecondsPart方法是在Java 9中引入的。Java
6、7和8的
输出与上面相同。
问题:这在 Java 6 和 7 中如何工作?
java.time
是内置的。链接
java.time.
java.time 首先被描述。
java.time
向后移植到 Java 6 和 7 (JSR-310 为 ThreeTen)。Java 9
This prints:
You are doing right in letting library methods do the conversions involved for you.
java.time
, the modern Java date and time API, or more precisely, itsDuration
class does it more elegantly and in a less error-prone way thanTimeUnit
.The
toMinutesPart
andtoSecondsPart
methods I used were introduced in Java 9.Java 6, 7 and 8
The output is the same as above.
Question: How can that work in Java 6 and 7?
java.time
comes built-in.org.threeten.bp
with subpackages.Links
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).4种实现的测试结果
必须对大量数据进行大量格式化,需要最佳性能,因此以下是(令人惊讶的)结果:
for (int i = 0; i < 1000000;我++){
函数调用
持续
时间:
formatTimeUnit:2216 毫秒
Test results for the 4 implementations
Having to do a lot of formatting for huge data, needed the best performance, so here are the (surprising) results:
for (int i = 0; i < 1000000; i++) {
FUNCTION_CALL
}
Durations:
formatTimeUnit: 2216 millis
标记为正确的答案有一个小错误,
例如,这是我得到的值的示例:
这是获得正确格式的解决方案是:
获得正确的格式:
获得格式的其他选项
hh:mm:ss
只是:The answer marked as correct has a little mistake,
for example this is an example of the value that i get:
This is the solution to get the right format is:
getting as a result a correct format:
other option to get the format
hh:mm:ss
is just :根据 Bohemian 的答案,我们不需要使用 TimeUnit 来查找已知值。
更优化的代码将是
希望它有帮助
Going by Bohemian's answer we need need not use TimeUnit to find a known value.
Much more optimal code would be
Hope it helps
格式:00:00:00.000
示例:615605 毫秒
00:10:15.605
Format: 00:00:00.000
Example: 615605 Millisecend
00:10:15.605
下面的代码以两种方式将
23:59:58:999 转换为 86398999
以及将
86398999 转换为 23:59:58:999
The code below does the conversion in both way
23:59:58:999 to 86398999
and than
86398999 to 23:59:58:999
我按照第一个答案中所示进行了尝试。它有效,但负号让我陷入困惑。 Groovy 我的回答:
I tried as shown in the first answer. It works, but minus brought me into confusion. My answer by Groovy:
对于 Kotlin
,其中
milSecs
是毫秒For Kotlin
where,
milSecs
is milliseconds在 kotlin 中
在 Java 中
In kotlin
In Java
好吧,你可以尝试这样的事情:
将毫秒转换为时间值
Well, you could try something like this, :
to convert milliseconds to a time value
在科特林中:
In Kotlin:
使用 Java 9 新的 Duration 方法 (toPart)
Using Java 9 new Duration methods (toPart)