将 java.time.Duration 漂亮地打印成类似“2 天”的内容
Pretty print java.time.Duration to something like "2 days"
以下:
java.time.Duration.ofDays(30).toString
产生这个输出:
PT720H
有没有简单的方法来漂亮地打印它,例如,打印为“30 天”。
同样,对于 "P2DT3H4M",我想将其输出为“2 天 3 小时 4 分钟”。
我知道这里的答案 "pretty print" duration in java,但不确定我是否可以 use/convert Java 的 Duration 到 Joda 的 Duration 来使用这个?
您可以java.time.Duration.ofDays(30).toDays()
接收持续时间中的天数,或java.time.Duration.ofDays(30).getSeconds()
秒。
如果您想要不同的格式,请使用 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));
这是文档:https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
我最终使用了这个(从这个答案扩展 )。
我确定应该有一种使用 Java/Scala 库的更简单的方法来做到这一点?请注意,我的输入将始终是 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(", "))
}
以下:
java.time.Duration.ofDays(30).toString
产生这个输出:
PT720H
有没有简单的方法来漂亮地打印它,例如,打印为“30 天”。
同样,对于 "P2DT3H4M",我想将其输出为“2 天 3 小时 4 分钟”。
我知道这里的答案 "pretty print" duration in java,但不确定我是否可以 use/convert Java 的 Duration 到 Joda 的 Duration 来使用这个?
您可以java.time.Duration.ofDays(30).toDays()
接收持续时间中的天数,或java.time.Duration.ofDays(30).getSeconds()
秒。
如果您想要不同的格式,请使用 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));
这是文档:https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
我最终使用了这个(从这个答案扩展 )。
我确定应该有一种使用 Java/Scala 库的更简单的方法来做到这一点?请注意,我的输入将始终是 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(", "))
}