如何使用joda时间将固定数量的毫秒格式化为hh:mm:ss?
我输入了 34600 毫秒,我想以 00:00:34 (HH:MM:SS) 的格式输出。
为此我应该查看 JDK/Joda-time 的哪些类?我需要它是高效的,最好是线程安全的,以避免每次解析时创建对象。
谢谢。
-- 编辑 --
使用此代码会产生时区敏感的结果,我如何确保格式是“自然的”,即使用绝对值。
import java.util.Locale;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormat.forPattern("kk:mm:ss").withLocale(new Locale("UTC"));
System.out.println(fmt.print(34600));
}
}
结果为 02:00:34 - +2h 是因为我的时区是 GMT+2。而预期输出是 00:00:34。
I have input, 34600 milliseconds I would like to output this in the format 00:00:34 (HH:MM:SS).
What classes should I look at JDK / Joda-time for this? I need this to be efficient, preferably thread safe to avoid object creations on each parsing.
Thank you.
-- EDIT --
Using this code produces time zone sensitive results, how can I make sure the formating is "natural", i.e. uses absolute values.
import java.util.Locale;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormat.forPattern("kk:mm:ss").withLocale(new Locale("UTC"));
System.out.println(fmt.print(34600));
}
}
Results in 02:00:34 - The +2h is because my time zone is GMT+2. While the expected output is 00:00:34.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 Joda 解决方案,请尝试一下(考虑到您更新的问题):
输出:
关于您对线程安全的偏好:
(来自PeriodFormatterBuilder文档)
For a Joda solution, give this a try (given your updated question):
Outputs:
Regarding your preference for thread safety:
(from PeriodFormatterBuilder documentation)
你可以使用 String.format 吗?
You could use String.format?