convert Duration To String in 00:00:00 format - Android java.util

Android examples for java.util:Date Time

Description

convert Duration To String in 00:00:00 format

Demo Code

public class Main {

  public static String converDurationToString(long duration) {
    String retVal = "00:00:00";

    if (duration <= 0) {
      return retVal;
    }//from w  ww  .ja  v  a2 s .  c om

    final long totalSeconds = duration / 1000;
    final long hours = totalSeconds / 3600;
    final long minutes = (totalSeconds % 3600) / 60;
    final long seconds = (totalSeconds % 3600) % 60;

    retVal = String.format("%02d:%02d:%02d", hours, minutes, seconds);

    return retVal;
  }

}

Related Tutorials