Robert Larsen
Posts: 0
Joined: Fri Aug 02, 2013 6:56 am

Java to convert seconds to hours.

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.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Java to convert seconds to hours.

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

Robert Larsen
Posts: 0
Joined: Fri Aug 02, 2013 6:56 am

Java to convert seconds to hours.

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; 

}

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Java to convert seconds to hours.

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

Return to “Issues”