如何向用户显示时间量? [英] How to display time amount to user?

查看:51
本文介绍了如何向用户显示时间量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要将时间量从毫秒转换为人类可读的字符串.

I want to convert time amount from milli-sec to a human readable string.

例如:

3,600,000 应该显示为 1:00:00 (1小时).

3,600,000 should be displayed as 1:00:00 (1 hour).

Java中是否存在可以做到这一点的现有库或类?

Is there an existing library or class in Java that can do that?

推荐答案

从1.5开始,就有java.util.concurrent.TimeUnit类,请像这样使用它:

Since 1.5 there is the java.util.concurrent.TimeUnit class, use it like this:

String.format("%d min, %d sec", 
    TimeUnit.MILLISECONDS.toMinutes(millis),
    TimeUnit.MILLISECONDS.toSeconds(millis) - 
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);

对于低于1.5的版本,您必须使用

For Versions below 1.5 You have to use

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

这篇关于如何向用户显示时间量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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