如何使用joda时间将固定数量的毫秒格式化为hh:mm:ss?

发布于 2024-10-28 03:10:53 字数 634 浏览 5 评论 0原文

我输入了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯寂 2024-11-04 03:10:53

对于 Joda 解决方案,请尝试一下(考虑到您更新的问题):

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));

输出:

00:00:34

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

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

(来自PeriodFormatterBuilder文档)

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)

半衾梦 2024-11-04 03:10:53

你可以使用 String.format 吗?

long millis = 34600;
long hours = millis/3600000;
long mins = millis/60000 % 60;
long second = millis / 1000 % 60;
String time = String.format("%02d:%02d:%02d", hours, mins, secs);

You could use String.format?

long millis = 34600;
long hours = millis/3600000;
long mins = millis/60000 % 60;
long second = millis / 1000 % 60;
String time = String.format("%02d:%02d:%02d", hours, mins, secs);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文