如何使用joda时间来将固定数量的millisecons格式化为hh:mm:ss? [英] How to use joda time to format a fixed number of millisecons to hh:mm:ss?

查看:371
本文介绍了如何使用joda时间来将固定数量的millisecons格式化为hh:mm:ss?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有34600毫秒的输入,我想以00:00:34(HH:MM:SS)的格式输出。

看JDK / Joda时间呢?我需要这是有效的,最好是线程安全的,以避免在每个解析对象创建。

谢谢。

- 编辑 -



使用此代码会产生时区敏感的结果,如何确保格式是自然的,即使用绝对值。 >

  import java.util.Locale; 
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

$ b 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。

解决方案

对于Joda解决方案,试试这个):
$ b $ pre $ PeriodFormatter fmt = new PeriodFormatterBuilder()
.printZeroAlways()
.minimumPrintedDigits(2)
.appendHours()
.appendSeparator(:)
.printZeroAlways()
.minimumPrintedDigits(2)
.appendMinutes()
.appendSeparator (:)
.printZeroAlways()
.minimumPrintedDigits(2)
.appendSeconds()
.toFormatter();
期间=新期间(34600);
System.out.println(fmt.print(period));

输出:

  00:00:34 

关于您对线程安全的偏好:


$ b


PeriodFormatterBuilder本身是
可变的,而不是线程安全的,但它构建的
格式化程序是
线程安全的和不可变的。


(from PeriodFormatterBuilder 文档)

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.

解决方案

For a Joda solution, give this a try (given your updated question):

PeriodFormatter fmt = new PeriodFormatterBuilder()
        .printZeroAlways()
        .minimumPrintedDigits(2)
        .appendHours()
        .appendSeparator(":")
        .printZeroAlways()
        .minimumPrintedDigits(2)
        .appendMinutes()
        .appendSeparator(":")
        .printZeroAlways()
        .minimumPrintedDigits(2)
        .appendSeconds()
        .toFormatter();
Period period = new Period(34600);
System.out.println(fmt.print(period));

Outputs:

00:00:34

Regarding your preference for thread safety:

PeriodFormatterBuilder itself is mutable and not thread-safe, but the formatters that it builds are thread-safe and immutable.

(from PeriodFormatterBuilder documentation)

这篇关于如何使用joda时间来将固定数量的millisecons格式化为hh:mm:ss?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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