我想打印持续时间,以毫秒为单位,具有不同的格式规范,具体取决于其大小:
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)
表示只有1小时的字段数字,持续时间低于10小时,但是当有一个领先的日期字段时,只有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
?
答案 0 :(得分:1)
也许不是真正的答案,但同时我担心您需要两个 PeriodFormatter
来解决此任务,因此请使用
object JodaTest {
import org.joda.time._
import format._
private def pdf(digits: Int) = new PeriodFormatter(
new PeriodFormatterBuilder()
.appendDays.appendSeparator("d ")
.printZeroAlways
.minimumPrintedDigits(digits).appendHours.appendSeparator(":")
.minimumPrintedDigits(2).appendMinutes
.toPrinter, null)
private lazy val pdf1 = pdf(1)
private lazy val pdf2 = pdf(2)
def durstr(duration: Long): String = {
val period = new Period(duration).normalizedStandard
val pdf = if (period.getDays > 0) pdf2 else pdf1
pdf.print(period)
}
}
导致期望的
2700000 => "0:45"
4980000 => "1:23"
43500000 => "12:05"
104940000 => "1d 05:09".
答案 1 :(得分:0)
仍然找到一个只有一个 PeriodFormatter
的解决方案,但在Joda-Time之外做了一些工作。
想法是
与
object JodaTest {
import org.joda.time._
import format._
// "000d 00:00" - 3 day digits for periods with up to 999 days long
private val pdf = new PeriodFormatter(new PeriodFormatterBuilder()
.printZeroAlways
.minimumPrintedDigits(3).appendDays.appendSeparator("d ")
.minimumPrintedDigits(2).appendHours.appendSeparator(":").appendMinutes
.toPrinter, null)
private def adjust(rawstr: String): String = {
// "000d 00:00" => ("000d 0", "0:00")
val (first, second) = rawstr splitAt 6
// remove unwanted leading zeros in first part, keep it in second
first.dropWhile(c => !c.isDigit || c == '0') + second
}
def durstr(duration: Long): String = {
// PeriodType.dayTime => day is the most significant field (no weeks etc.)
adjust(pdf.print(new Period(duration) normalizedStandard PeriodType.dayTime))
}
}
导致
duration => rawstr => adjust
0 => "000d 00:00" => "0:00"
2700000 => "000d 00:45" => "0:45"
4980000 => "000d 01:23" => "1:23"
43500000 => "000d 12:05" => "12:05"
104940000 => "001d 05:09" => "1d 05:09"
518760000 => "006d 00:06" => "6d 00:06"
605220000 => "007d 00:07" => "7d 00:07"
951060000 => "011d 00:11" => "11d 00:11"
43230000000 => "500d 08:20" => "500d 08:20"
当然,通过指定类似Excel数字格式(#,## 0.00#)的模式来直接使用Joda-Time构建这样的格式会很好,可以说明需要零的地方或仅在需要时。但似乎并不清楚如何完全定义它,因为你不仅有'0'和'#',而且需要为每个字段添加字符,并将文字放入格式字符串(也许通过转义)也会很好。
答案 2 :(得分:0)
您可以实现PeriodPrinter
界面,根据需要格式化句点,然后使用the builder设置格式化程序。