aleroot aleroot - 5 months ago 14
Java Question

Get application uptime

I've written an application in java and I want to add a feature to report the uptime of the application. Is there a Class/method in the JVM that can do this?

Should I save the timestamp when application starts, then calculate the difference with the current uptime in the moment of the request?

What's a better way to retrieve application uptime in Java?

Answer

You can use RuntimeMXBean.getUptime()

RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
long uptime = rb.getUptime();