我困惑。在偶然发现这个线程后,我试图弄清楚如何格式化一个具有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))。
在kotlin
private fun stringForTime(timeMs: Int): String {
val totalSeconds = timeMs / 1000
val seconds = totalSeconds % 60
val minutes = totalSeconds / 60 % 60
val hours = totalSeconds / 3600
return if (hours > 0) {
"%d:%02d:%02d".format(hours, minutes, seconds)
} else {
"%02d:%02d".format(minutes, seconds)
}
}
在Java中
private String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = totalSeconds / 60 % 60;
int hours = totalSeconds / 3600;
return hours > 0 ? String.format(Locale.getDefault(),
"%d:%02d:%02d",
hours,
minutes,
seconds) :
String.format(Locale.getDefault(),
"%02d:%02d",
minutes,
seconds);
}
Java 9
Duration timeLeft = Duration.ofMillis(3600000);
String hhmmss = String.format("%02d:%02d:%02d",
timeLeft.toHours(), timeLeft.toMinutesPart(), timeLeft.toSecondsPart());
System.out.println(hhmmss);
这个打印:
01:00:00
让库方法为您完成相关的转换是正确的。java。time是现代Java日期和时间API,或者更准确地说,它的Duration类比TimeUnit更优雅,更不容易出错。
我使用的toMinutesPart和toSecondsPart方法是在Java 9中引入的。
Java 6、7和8
long hours = timeLeft.toHours();
timeLeft = timeLeft.minusHours(hours);
long minutes = timeLeft.toMinutes();
timeLeft = timeLeft.minusMinutes(minutes);
long seconds = timeLeft.toSeconds();
String hhmmss = String.format("%02d:%02d:%02d", hours, minutes, seconds);
System.out.println(hhmmss);
输出与上面相同。
问题:这在Java 6和7中如何工作?
在Java 8及以上版本和更新的Android设备上(我被告知从API级别26开始)。时间是与生俱来的。
在Java 6和7中获得ThreeTen Backport,现代类的后端口(JSR 310的ThreeTen;参见底部的链接)。
在(旧的)Android上使用ThreeTen Backport的Android版本。叫做ThreeTenABP。并确保从org.three .bp导入带有子包的日期和时间类。
链接
Oracle教程:Date Time解释如何使用java.time。
Java规范请求(JSR) 310,其中Java。时间是最早被描述的。
ThreeTen Backport项目,java的Backport。timeto Java 6和7 (JSR-310为ThreeTen)。
ThreeTenABP, Android版的ThreeTen Backport
问:如何在Android项目中使用ThreeTenABP,并有一个非常详细的解释。