我想用H:MM:SS这样的模式以秒为单位格式化持续时间。java中当前的实用程序设计用于格式化时间,而不是持续时间。


当前回答

有一个相当简单和优雅的方法,至少在24小时内:

DateTimeFormatter.ISO_LOCAL_TIME.format(value.addTo(LocalTime.of(0, 0)))

格式化程序需要一个时态对象来格式化,因此可以通过将持续时间添加到LocalTime为00:00(即午夜)来创建一个时态对象。这将为您提供一个LocalTime,表示从午夜到该时间的持续时间,然后很容易将其格式化为标准HH:mm:ss表示法。这样做的优点是不需要外部库,并且使用java。时间库做计算,而不是手动计算小时、分、秒。

其他回答

我使用Apache common的DurationFormatUtils,就像这样:

DurationFormatUtils.formatDuration(millis, "**H:mm:ss**", true);

这是一个可行的选择。

public static String showDuration(LocalTime otherTime){          
    DateTimeFormatter df = DateTimeFormatter.ISO_LOCAL_TIME;
    LocalTime now = LocalTime.now();
    System.out.println("now: " + now);
    System.out.println("otherTime: " + otherTime);
    System.out.println("otherTime: " + otherTime.format(df));

    Duration span = Duration.between(otherTime, now);
    LocalTime fTime = LocalTime.ofNanoOfDay(span.toNanos());
    String output = fTime.format(df);

    System.out.println(output);
    return output;
}

使用

System.out.println(showDuration(LocalTime.of(9, 30, 0, 0)));

产生如下内容:

otherTime: 09:30
otherTime: 09:30:00
11:31:27.463
11:31:27.463

查看所有这些计算,大多数单位(小时、分钟等)都有一个. tofoopart()方便方法,这可能是有帮助的。

E.g.

Duration.ofMinutes(110L).toMinutesPart() == 50

读:到父单位(小时)的下一个值的分钟数。

在scala中,不需要库:

def prettyDuration(str:List[String],seconds:Long):List[String]={
  seconds match {
    case t if t < 60 => str:::List(s"${t} seconds")
    case t if (t >= 60 && t< 3600 ) => List(s"${t / 60} minutes"):::prettyDuration(str, t%60)
    case t if (t >= 3600 && t< 3600*24 ) => List(s"${t / 3600} hours"):::prettyDuration(str, t%3600)
    case t if (t>= 3600*24 ) => List(s"${t / (3600*24)} days"):::prettyDuration(str, t%(3600*24))
  }
}
val dur = prettyDuration(List.empty[String], 12345).mkString("")

那么下面的函数呢 + H: MM: SS 或 + H: MM: SS.sss

public static String formatInterval(final long interval, boolean millisecs )
{
    final long hr = TimeUnit.MILLISECONDS.toHours(interval);
    final long min = TimeUnit.MILLISECONDS.toMinutes(interval) %60;
    final long sec = TimeUnit.MILLISECONDS.toSeconds(interval) %60;
    final long ms = TimeUnit.MILLISECONDS.toMillis(interval) %1000;
    if( millisecs ) {
        return String.format("%02d:%02d:%02d.%03d", hr, min, sec, ms);
    } else {
        return String.format("%02d:%02d:%02d", hr, min, sec );
    }
}