问题描述
我很困惑.在偶然发现 this 线程后,我试图弄清楚如何格式化一个具有 hh:mm:ss
格式的倒数计时器.
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
.
这是我的尝试 -
//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代码>.显然我的逻辑有问题,但目前我看不出是什么!
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!
有人可以帮忙吗?
编辑 -
修复了它.这是将毫秒格式化为 hh:mm:ss
格式的正确方法 -
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))));
问题出在这个 TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
.应该是这个 TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(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.
推荐答案
你真的很亲近:
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
您使用分钟而不是小时将小时转换为毫秒.
You were converting hours to millisseconds using minutes instead of hours.
顺便说一句,我喜欢你使用 TimeUnit
API :)
BTW, I like your use of the TimeUnit
API :)
这是一些测试代码:
public static void main(String[] args) throws ParseException {
long millis = 3600000;
String hms = 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)));
System.out.println(hms);
}
输出:
01:00:00
我意识到通过使用模数除法而不是减法可以大大简化我上面的代码:
I realised that my code above can be greatly simplified by using a modulus division instead of subtraction:
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1));
仍然对所有魔法值使用 TimeUnit
API,并给出完全相同的输出.
Still using the TimeUnit
API for all magic values, and gives exactly the same output.
这篇关于如何将毫秒转换为“hh:mm:ss"格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!