Java 漂亮打印持续时间

发布于 2024-09-16 00:46:15 字数 182 浏览 3 评论 0原文

是否有 Java 类或一些示例代码可以将 java 日期或时间戳转换为类似的内容:

"3 hours"
" 20 seconds"
 "25 minutes"

我需要在我的 Web 应用程序中使用这些字符串来显示生成文件所需的时间(当然以漂亮的打印方式:))

谢谢,

Is there Java class or some sample code that can convert a java Date or Timestamp into something like:

"3 hours"
" 20 seconds"
 "25 minutes"

I need those strings in my web application to show how much it took to generate a file (in a pretty print way of course :) )

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

三生殊途 2024-09-23 00:46:16

使用 JodaTime 是最好的整体方法,但这是一种不使用任何特定于域的库的方法,使用 ChoiceFormat 间接在 MessageFormat

static String choiceFor(int index, String noun) {
    return "{index,choice,0#|1#1 noun |1<{index,number,integer} nouns }"
        .replace("index", String.valueOf(index))
        .replace("noun", noun);
}
static String prettyPrint(int h, int m, int s) {
    String fmt = 
        choiceFor(0, "hour") +
        choiceFor(1, "minute") +
        choiceFor(2, "second");
    return java.text.MessageFormat.format(fmt, h, m, s).trim();
}

现在您可以拥有 (如 ideone.com 上所示):

    System.out.println(prettyPrint(1,2,3));
    // 1 hour 2 minutes 3 seconds

    System.out.println(prettyPrint(0,0,7));
    // 7 seconds

    System.out.println(prettyPrint(1,0,1));
    // 1 hour 1 second

    System.out.println(prettyPrint(0,2,0));
    // 2 minutes

您当然可以扩展此以包括天/月/年/等。

Using JodaTime is the best overall approach, but here's one way to do it without using any domain-specific libraries, using the ChoiceFormat indirectly in the context of a MessageFormat:

static String choiceFor(int index, String noun) {
    return "{index,choice,0#|1#1 noun |1<{index,number,integer} nouns }"
        .replace("index", String.valueOf(index))
        .replace("noun", noun);
}
static String prettyPrint(int h, int m, int s) {
    String fmt = 
        choiceFor(0, "hour") +
        choiceFor(1, "minute") +
        choiceFor(2, "second");
    return java.text.MessageFormat.format(fmt, h, m, s).trim();
}

Now you can have (as seen on ideone.com):

    System.out.println(prettyPrint(1,2,3));
    // 1 hour 2 minutes 3 seconds

    System.out.println(prettyPrint(0,0,7));
    // 7 seconds

    System.out.println(prettyPrint(1,0,1));
    // 1 hour 1 second

    System.out.println(prettyPrint(0,2,0));
    // 2 minutes

You can of course extend this to include days/months/years/etc.

两相知 2024-09-23 00:46:16

您可以从 java Calendar 类获取小时、分钟和秒,然后将它们与您喜欢的内容(小时、分钟...)连接起来

Calendar c = Calendar.getInstance();
c.setTimeinMillis(<the time in milli second format (a long number)>);

int hours = c.get(Calendar.HOUR);
int mins = c.get(Calendar.MIN);
...

you can get the hours, minutes and seconds from java Calendar class and then concat them with what ever you like (hours , mins ...)

Calendar c = Calendar.getInstance();
c.setTimeinMillis(<the time in milli second format (a long number)>);

int hours = c.get(Calendar.HOUR);
int mins = c.get(Calendar.MIN);
...
地狱即天堂 2024-09-23 00:46:16

这听起来很像 TimeAgo。您也许可以利用其中的代码来格式化持续时间,如下所示。

This sounds very much like TimeAgo. You may be able to leverage the code in there to format durations like this.

┈┾☆殇 2024-09-23 00:46:15

使用JodaTime

PeriodFormat.getDefault( ).print( Hours.THREE );
PeriodFormat.getDefault( ).print( Seconds.seconds( 25 ) );
PeriodFormat.getDefault( ).print( Minutes.minutes( 20 ) );

PS也很容易获得2个时间点之间的小时/秒/分钟数。

With JodaTime:

PeriodFormat.getDefault( ).print( Hours.THREE );
PeriodFormat.getDefault( ).print( Seconds.seconds( 25 ) );
PeriodFormat.getDefault( ).print( Minutes.minutes( 20 ) );

P.S. Also it's very easy to get the number of hours/seconds/minutes between 2 time points.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文