Tuesday 2 October 2012

Convert milliseconds to HH:MM:SS String in java


  1. long timeMillis = ...;  
  2. long time = timeMillis / 1000;  
  3. String seconds = Integer.toString((int)(time % 60));  
  4. String minutes = Integer.toString((int)((time % 3600) / 60));  
  5. String hours = Integer.toString((int)(time / 3600));  
  6. for (int i = 0; i < 2; i++) {  
  7. if (seconds.length() < 2) {  
  8. seconds = "0" + seconds;  
  9. }  
  10. if (minutes.length() < 2) {  
  11. minutes = "0" + minutes;  
  12. }  
  13. if (hours.length() < 2) {  
  14. hours = "0" + hours;  
  15. }  

No comments:

Post a Comment