[英]Pretty print java.time.Duration to something like “2 days”
The following: 下列:
java.time.Duration.ofDays(30).toString
produces this output: 产生以下输出:
PT720H
Is there a simple way to pretty print this, for example, as "30 days". 有没有一种简单的方法可以漂亮地打印出来,例如“ 30 days”。
Likewise, for something like "P2DT3H4M", I'd like to output this as "2 days, 3 hours and 4 minutes". 同样,对于“ P2DT3H4M”之类的东西,我想将其输出为“ 2天3小时4分钟”。
I'm aware of the answer here "pretty print" duration in java , but not sure if I can use/convert Java's Duration to Joda's Duration to be able to use this? 我知道在Java中“漂亮打印”持续时间的答案,但是不确定是否可以使用Java的持续时间/将其转换为Joda的持续时间才能使用?
You can do java.time.Duration.ofDays(30).toDays()
to receive the amount of days in the Duration, or java.time.Duration.ofDays(30).getSeconds()
for seconds. 你可以做
java.time.Duration.ofDays(30).toDays()
接收的持续天量,或java.time.Duration.ofDays(30).getSeconds()
为秒。
If you'd like to format it differently, use String.format(), so something like this: 如果您想使用其他格式,请使用String.format(),如下所示:
Duration dur = Duration.parse("P2DT3H4M5.6S");
Long s = dur.getSeconds();
String formatted = String.format("%d days, %02d hours, %02d minutes", s / 3600, (s % 3600) / 60, (s % 60));
Here's the doc: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html 这是文档: https : //docs.oracle.com/javase/8/docs/api/java/time/Duration.html
I ended up using this (extended from this answer https://stackoverflow.com/a/49628638/5300930 ). 我最终使用了这个(从这个答案https://stackoverflow.com/a/49628638/5300930扩展)。
I'm sure that there should be a simpler way to do this using the Java/Scala libraries? 我确定应该有使用Java / Scala库执行此操作的更简单方法? Note, my input will always be a java.time.Duration, I can use the Joda libraries, but they must be able to start with a java.time.Duration as input.
注意,我的输入将始终是java.time.Duration,我可以使用Joda库,但是它们必须能够以java.time.Duration作为输入。
/** Encodes a [[java.time.Duration]] into a more readable format.
*
* @example
* encodeDuration(Duration.ofDays(30).plusHours(3)) // returns "30 days, 3 hours"
* encodeDuration(Duration.ofDays(1)) // returns "1 day"
* encodeDuration(Duration.parse("PT720H")) // returns "30 days"
* encodeDuration(Duration.parse("PT12000H10S")) // returns "1 year, 4 months, 15 days, 10 seconds"
*/
implicit val encodeDuration: Encoder[java.time.Duration] = (duration: Duration) => {
val oneMinute = 60
val twoMinutes = oneMinute * 2
val oneHour = oneMinute * 60
val twoHours = oneHour * 2
val oneDay = oneHour * 24
val twoDays = oneDay * 2
val oneMonth = oneDay * 30
val twoMonths = oneMonth * 2
val oneYear = oneDay * 365
val twoYears = oneYear * 2
// scalastyle:off cyclomatic.complexity
def encodeDuration(result: List[String], seconds: Long): List[String] = {
seconds match {
case seconds if seconds <= 0 =>
List.empty[String]
case seconds if seconds == 1 =>
result ::: List(s"${seconds} second")
case seconds if seconds < oneMinute =>
result ::: List(s"${seconds} seconds")
case seconds if seconds >= oneMinute && seconds < twoMinutes =>
List(s"${seconds / oneMinute} minute") ::: encodeDuration(result, seconds % oneMinute)
case seconds if seconds >= oneMinute && seconds < oneHour =>
List(s"${seconds / oneMinute} minutes") ::: encodeDuration(result, seconds % oneMinute)
case seconds if seconds >= oneHour && seconds < twoHours =>
List(s"${seconds / oneHour} hour") ::: encodeDuration(result, seconds % oneHour)
case seconds if seconds >= twoHours && seconds < oneDay =>
List(s"${seconds / oneHour} hours") ::: encodeDuration(result, seconds % oneHour)
case seconds if seconds >= oneDay && seconds < twoDays =>
List(s"${seconds / oneDay} day") ::: encodeDuration(result, seconds % oneDay)
case seconds if seconds >= twoDays && seconds < oneMonth =>
List(s"${seconds / oneDay} days") ::: encodeDuration(result, seconds % oneDay)
case seconds if seconds >= oneMonth && seconds < twoMonths =>
List(s"${seconds / oneMonth} month") ::: encodeDuration(result, seconds % oneMonth)
case seconds if seconds >= twoMonths && seconds < oneYear =>
List(s"${seconds / oneMonth} months") ::: encodeDuration(result, seconds % oneMonth)
case seconds if seconds >= oneYear && seconds < twoYears =>
List(s"${seconds / oneYear} year") ::: encodeDuration(result, seconds % oneYear)
case seconds if seconds >= twoYears =>
List(s"${seconds / oneYear} years") ::: encodeDuration(result, seconds % oneYear)
}
}
// scalastyle:on cyclomatic.complexity
Encoder.encodeString(encodeDuration(List.empty[String], duration.getSeconds).mkString(", "))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.