这是我的代码,用于显示应用程序的正常运行时间:
/**
* Gets the uptime of the application since the JVM launch
* @return The uptime in a formatted String (DAYS, MONTHS, MINUTES, SECONDS)
*/
public static String getGameUptime() {
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
DateFormat dateFormat = new SimpleDateFormat("dd:HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
return dateFormat.format(new Date(mxBean.getUptime()));
}
但是当应用程序只运行了3分50秒时,它将返回“01:00:03:50”。在这种情况下,01 应为 00。这是什么原因呢?我该如何解决这个问题?
最佳答案
试试这个
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long uptime = mxBean.getUptime();
String d = uptime / (3600 * 1000 * 24) + ":" + dateFormat.format(uptime);
请注意,DateFormat 也接受毫秒
关于java - ManagementFactory.getRuntimeMXBean().getUptime() 格式化以显示日、小时、分钟和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614780/