将joda-time seconds转换为格式化的时间字符串 [英] convert joda-time seconds to formatted time string

查看:141
本文介绍了将joda-time seconds转换为格式化的时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

say I have 110 seconds that I want to convert to 01:50 or 00:01:50 or such. How do I do that in joda-time? I load the number into Seconds but then toString is not doing the conversion for me.

推荐答案

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 ) - 另请参阅@Isaksson的正确答案。

Note this answer is valid for amounts of seconds less than one full day (86400). If you have bigger numbers then better use a formatter for durations (in Joda-Time called PeriodFormatter) - see also the right answer of @Isaksson.

这篇关于将joda-time seconds转换为格式化的时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆