15

I want to get the JVM start time and uptime. So far I have done this:

public long getjvmstarttime(){
    final long uptime = ManagementFactory.getRuntimeMXBean().getStartTime();
    return uptime;
}

public long getjvmuptime(){
    final long uptime = ManagementFactory.getRuntimeMXBean().getUptime();
    return uptime;
}

But I get the time in milliseconds. How I can convert the time in days and hours. I want to display the milliseconds in this format: 3 days, 8 hours, 32 minutes. Is there amy internal Java method that can convert the milliseconds?

user1285928
  • 1,328
  • 29
  • 98
  • 147

4 Answers4

60

Once you have the time in milliseconds you can use the TimeUnit enum to convert it to other time units. Converting to days will just require one call.

long days = TimeUnit.MILLISECONDS.toDays(milliseconds);

Getting the hours will involve another similar call for the total hours, then computing the left over hours after the days are subtracted out.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • thanks dude, you are the boss.. so much better than the accepted answer – Marko Niciforovic Mar 23 '15 at 10:59
  • Best answer, still the method above and pretty much all the other like .toHours(millis) return a long value, why? – Lampione Mar 06 '16 at 12:47
  • @VeenarM These solutions were provided on the same day, and TimeUnit has been around since Java 5. (DAYS was added to it with Java 7, which would have been a year before these posts.) – Bill the Lizard Jul 21 '17 at 02:57
  • O wow my bad - some reason just assumed it was part of the new DateTime components - then this is awesome and much better solution :) – VeenarM Jul 21 '17 at 10:10
40

The code below does the math you need and builds the resulting string:

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());
obataku
  • 29,212
  • 3
  • 44
  • 57
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    Your if statements should be checking if ms is greater or equal to DAY/MINUTE/HOUR/SECOND. For instance if ms=1000, it will show as 0 seconds. – MrK Jan 29 '17 at 02:48
2

Take a look at Pretty Time. It's a library for generating human reabable time strings from timestamps like milliseconds.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
1

There below code cleanly does the job

package com.company;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) {
        final long milliseconds;
        Scanner in = new Scanner(System.in);

        System.out.println("Enter time in milliseconds: ");
        milliseconds = in.nextLong();

        final long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);

        final long minute = TimeUnit.MILLISECONDS.toMinutes(milliseconds);

        final long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);

        final long days = TimeUnit.MILLISECONDS.toDays(milliseconds);

        if(milliseconds < 1000){
            System.out.println(days +"d:" +hours+"h:" +minute+"m:" +seconds +"s:" +milliseconds +"ms");
        }
        else{
            System.out.println(days +"d:" +hours % 24 +"h:" +minute % 60 +"m:" +seconds % 60 +"s");
        }
    }
}

Sample output

Enter time in milliseconds: 12000000

0d:3h:20m:0s

Dharman
  • 30,962
  • 25
  • 85
  • 135
ARMEL FOPA
  • 206
  • 3
  • 8