Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ManagementFactory.getRuntimeMXBean().getUptime()格式化以显示天、小时、分钟和秒_Java_Date_Simpledateformat_Uptime - Fatal编程技术网

Java ManagementFactory.getRuntimeMXBean().getUptime()格式化以显示天、小时、分钟和秒

Java ManagementFactory.getRuntimeMXBean().getUptime()格式化以显示天、小时、分钟和秒,java,date,simpledateformat,uptime,Java,Date,Simpledateformat,Uptime,这是显示应用程序正常运行时间的代码: /** * 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.get

这是显示应用程序正常运行时间的代码:

/**
 * 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也接受毫秒