将joda-time seconds转换为格式化的时间字符串

说我有110秒,我想转换为01:5000:01:50等。 我如何在joda-time中这样做? 我将数字加载到Seconds但是toString没有为我做转换。

 LocalTime time = new LocalTime(0, 0); // midnight time = time.plusSeconds(110); String output = DateTimeFormat.forPattern("HH:mm:ss").print(time); System.out.println(output); // 00:01:50 

请注意,此答案适用于小于一整天(86400)的秒数。 如果您有更大的数字,那么最好在格式化程序中使用格式化程序(在Joda-Time中称为PeriodFormatter ) – 另请参阅PeriodFormatter的正确答案。

您可以为您的格式构建格式化程序并使用它。 要以秒为单位将时间标准化为分钟/秒,请使用normalizedStandard() ;

 PeriodFormatter myFormat = new PeriodFormatterBuilder() .printZeroAlways().minimumPrintedDigits(2).appendMinutes() .appendSeparator(":") .printZeroAlways().minimumPrintedDigits(2).appendSeconds() .toFormatter(); Period period = Period.seconds(110).normalizedStandard(); System.out.println(myFormat.print(period)); > 01:50 
 public static void main(String[] args) { Date dt = new Date(); String date = String.format("%tH:%tM", dt, dt); System.out.println(date); // or System.out.printf("%tH:%tM", dt, dt); }