漂亮地将java.time.Duration打印为“ 2 days”

时间:2018-08-01 16:18:13

标签: java scala jodatime

以下:

java.time.Duration.ofDays(30).toString

产生以下输出:

PT720H

是否有一种简单的方法可以漂亮地打印出来,例如“ 30天”。

同样,对于“ P2DT3H4M”之类的东西,我想将其输出为“ 2天3小时4分钟”。

我知道这里的答案"pretty print" duration in java,但不确定是否可以使用Java的工时/将其转换为Joda的工时才能使用?

2 个答案:

答案 0 :(得分:0)

您可以执行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

答案 1 :(得分:0)

我最终使用了它(从这个答案https://stackoverflow.com/a/49628638/5300930扩展过来)。

我确定使用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(", "))
  }