Page 1 of 1

Java to convert seconds to hours.

Posted: Mon Nov 25, 2013 12:33 am
by Robert Larsen

My Service Call gives me a time display in seconds ex.(30042), i then map it to a label. Is there any java I can add to change it to a time format like this (08:20:42). I have tried some code from sources I googled but none would work.


Java to convert seconds to hours.

Posted: Mon Nov 25, 2013 12:49 am
by maxkatz

Can you show us the code that you tried? You can also look if this library might help you: http://momentjs.com/


Java to convert seconds to hours.

Posted: Mon Nov 25, 2013 1:07 am
by Robert Larsen

String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second parm
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);

Code: Select all

 if (hours   < 10) {hours   = "0"+hours;} 
 if (minutes < 10) {minutes = "0"+minutes;} 
 if (seconds < 10) {seconds = "0"+seconds;} 
 var time    = hours+':'+minutes+':'+seconds; 
 return time; 

}


Java to convert seconds to hours.

Posted: Mon Nov 25, 2013 1:15 am
by maxkatz

Did you debug it line by line to make sure you get correct values..?