我想打印以毫秒为单位的持续时间,并根据其大小使用不同的格式规范:
case (1) "H:mm" if duration < 10 hours
case (2) "HH:mm" if duration < 24 hours
case (3) "#d HH:mm" else (duration >= 24 hours)
这意味着持续时间低于 10 小时的只有 1 小时字段数字,
但有前导日字段时只有 2 小时字段数字!
例子:
case (1) "0:45" means 45 minutes,
"1:23" means 1 hour and 23 minutes,
case (2) "12:05" means 12 hours and 5 minutes and
case (3) "1d 05:09" means 1 day, 5 hours and 9 minutes
(= 29 hours and 9 minutes).
我试过
object JodaTest {
import org.joda.time._
private val pdf = {
import format._
val pfb = new PeriodFormatterBuilder()
.appendDays.appendSeparator("d ")
.printZeroAlways
.minimumPrintedDigits(2).appendHours.appendSeparator(":")
.appendMinutes
new PeriodFormatter(pfb.toPrinter, null)
}
def durstr(duration: Long): String =
pdf.print((new Period(duration)).normalizedStandard)
}
这导致
2700000 => "00:45" but should be "0:45"
4980000 => "01:23" but should be "1:23"
43500000 => "12:05"
104940000 => "1d 05:09"
但我不知道如何在情况(1)中省略两位数日表示的前导零,但同时强制在情况(3)中使用相同的 PeriodFormat 打印它。
有没有可能用一个来做到这一点org.joda.time.format.PeriodFormatter
?