Saturday, 24 November 2012

Java: Converting millisecond to hours and minutes


I was looking at the way and found it in the stackoverflow website:

public String convertMS(float ms) {
   int seconds = (int) ((ms / 1000) % 60);
   int minutes = (int) (((ms / 1000) / 60) % 60);
   int hours = (int) ((((ms / 1000) / 60) / 60) % 24);


String sec, min, hrs;
    if(seconds<10)  sec="0"+seconds;
    else            sec= ""+seconds;
    if(minutes<10)  min="0"+minutes;
    else            min= ""+minutes;
    if(hours<10)    hrs="0"+hours;
    else            hrs= ""+hours;


   if(hours == 0)  return minutes+":"+seconds + " MM:SS";
   else    return hours+":"+minutes+":"+minutes + "HH:MM:SS";

}

Here is the conversion:


x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
x /= 24
days = x

No comments:

Post a Comment