Java Program To Convert Duration in Milliseconds To HH:MM:SS Format

Posted on the 23 September 2013 by Abhishek Somani @somaniabhi
This Java Program Converts duration in MilliSeconds to "HH:MM:SS" format .
import java.util.concurrent.TimeUnit;

public class MiliSecondsConverter {
 public static void main(String[] args) throws InterruptedException {

  long millis = System.currentTimeMillis();
  Thread.sleep(5000);
  millis = System.currentTimeMillis() - millis;

  
  System.out.println(new MiliSecondsConverter().convert(millis));
 }
 
 public String convert(long miliSeconds)
 {
  int hrs = (int) TimeUnit.MILLISECONDS.toHours(miliSeconds) % 24;
  int min = (int) TimeUnit.MILLISECONDS.toMinutes(miliSeconds) % 60;
  int sec = (int) TimeUnit.MILLISECONDS.toSeconds(miliSeconds) % 60;
  return String.format("%02d:%02d:%02d", hrs, min, sec);
 }
}
Post Comments !!