如何把毫秒转换成小时和天?
我想获取JVM的启动时间和正常运行时间。到目前为止,我已经做到了:
public long getjvmstarttime(){ final long uptime = ManagementFactory.getRuntimeMXBean().getStartTime();
return uptime;
}
public long getjvmuptime(){
final long uptime = ManagementFactory.getRuntimeMXBean().getUptime();
return uptime;
}
但是我得到的时间以毫秒为单位。如何将时间转换为天数和小时数。我想以这种格式显示毫秒:3天8小时32分钟。是否有可以转换毫秒数的amy内部Java方法?
回答:
下面的代码执行所需的数学运算并生成结果字符串:
private static final int SECOND = 1000;private static final int MINUTE = 60 * SECOND;
private static final int HOUR = 60 * MINUTE;
private static final int DAY = 24 * HOUR;
// TODO: this is the value in ms
long ms = 10304004543l;
StringBuffer text = new StringBuffer("");
if (ms > DAY) {
text.append(ms / DAY).append(" days ");
ms %= DAY;
}
if (ms > HOUR) {
text.append(ms / HOUR).append(" hours ");
ms %= HOUR;
}
if (ms > MINUTE) {
text.append(ms / MINUTE).append(" minutes ");
ms %= MINUTE;
}
if (ms > SECOND) {
text.append(ms / SECOND).append(" seconds ");
ms %= SECOND;
}
text.append(ms + " ms");
System.out.println(text.toString());
以上是 如何把毫秒转换成小时和天? 的全部内容, 来源链接: utcz.com/qa/427758.html