Real'sHowTo AddThis Feed Button
Custom Search

Format a duration in milliseconds into a human-readable formatTag(s): Date and Time


Given a time in ms, this HowTo will output the long form "<w> days, <x> hours, <y> minutes and (z) seconds" and the short form "<dd:>hh:mm:ss".
public class TimeUtils {

  public final static long ONE_SECOND = 1000;
  public final static long SECONDS = 60;

  public final static long ONE_MINUTE = ONE_SECOND * 60;
  public final static long MINUTES = 60;

  public final static long ONE_HOUR = ONE_MINUTE * 60;
  public final static long HOURS = 24;

  public final static long ONE_DAY = ONE_HOUR * 24;

  private TimeUtils() {
  }

  /**
   * converts time (in milliseconds) to human-readable format
   *  "<w> days, <x> hours, <y> minutes and (z) seconds"
   */
  public static String millisToLongDHMS(long duration) {
    StringBuffer res = new StringBuffer();
    long temp = 0;
    if (duration >= ONE_SECOND) {
      temp = duration / ONE_DAY;
      if (temp > 0) {
        duration -= temp * ONE_DAY;
        res.append(temp).append(" day").append(temp > 1 ? "s" : "")
           .append(duration >= ONE_MINUTE ? ", " : "");
      }

      temp = duration / ONE_HOUR;
      if (temp > 0) {
        duration -= temp * ONE_HOUR;
        res.append(temp).append(" hour").append(temp > 1 ? "s" : "")
           .append(duration >= ONE_MINUTE ? ", " : "");
      }

      temp = duration / ONE_MINUTE;
      if (temp > 0) {
        duration -= temp * ONE_MINUTE;
        res.append(temp).append(" minute").append(temp > 1 ? "s" : "");
      }

      if (!res.toString().equals("") && duration >= ONE_SECOND) {
        res.append(" and ");
      }

      temp = duration / ONE_SECOND;
      if (temp > 0) {
        res.append(temp).append(" second").append(temp > 1 ? "s" : "");
      }
      return res.toString();
    } else {
      return "0 second";
    }
  }

  /**
   * converts time (in milliseconds) to human-readable format
   *  "<dd:>hh:mm:ss"
   */
  public static String millisToShortDHMS(long duration) {
    String res = "";
    duration /= ONE_SECOND;
    int seconds = (int) (duration % SECONDS);
    duration /= SECONDS;
    int minutes = (int) (duration % MINUTES);
    duration /= MINUTES;
    int hours = (int) (duration % HOURS);
    int days = (int) (duration / HOURS);
    if (days == 0) {
      res = String.format("%02d:%02d:%02d", hours, minutes, seconds);
    } else {
      res = String.format("%dd%02d:%02d:%02d", days, hours, minutes, seconds);
    }
    return res;
  }

  public static void main(String args[]) {
    System.out.println(millisToLongDHMS(123));
    System.out.println(millisToLongDHMS((5 * ONE_SECOND) + 123));
    System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR));
    System.out.println(millisToLongDHMS(ONE_DAY + 2 * ONE_SECOND));
    System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR + (2 * ONE_MINUTE)));
    System.out.println(millisToLongDHMS((4 * ONE_DAY) + (3 * ONE_HOUR)
        + (2 * ONE_MINUTE) + ONE_SECOND));
    System.out.println(millisToLongDHMS((5 * ONE_DAY) + (4 * ONE_HOUR)
        + ONE_MINUTE + (23 * ONE_SECOND) + 123));
    System.out.println(millisToLongDHMS(42 * ONE_DAY));
    /*
      output :
            0 second
            5 seconds
            1 day, 1 hour
            1 day and 2 seconds
            1 day, 1 hour, 2 minutes
            4 days, 3 hours, 2 minutes and 1 second
            5 days, 4 hours, 1 minute and 23 seconds
            42 days
     */
    System.out.println(millisToShortDHMS(123));
    System.out.println(millisToShortDHMS((5 * ONE_SECOND) + 123));
    System.out.println(millisToShortDHMS(ONE_DAY + ONE_HOUR));
    System.out.println(millisToShortDHMS(ONE_DAY + 2 * ONE_SECOND));
    System.out
        .println(millisToShortDHMS(ONE_DAY + ONE_HOUR + (2 * ONE_MINUTE)));
    System.out.println(millisToShortDHMS((4 * ONE_DAY) + (3 * ONE_HOUR)
        + (2 * ONE_MINUTE) + ONE_SECOND));
    System.out.println(millisToShortDHMS((5 * ONE_DAY) + (4 * ONE_HOUR)
        + ONE_MINUTE + (23 * ONE_SECOND) + 123));
    System.out.println(millisToShortDHMS(42 * ONE_DAY));
    /*
    output :
            00:00:00
            00:00:05
            1d01:00:00
            1d00:00:02
            1d01:02:00
            4d03:02:01
            5d04:01:23
            42d00:00:00

   */
  }
}

Using java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit;
...

  /**
   * converts time (in milliseconds) to human-readable format
   *  "<dd:>hh:mm:ss"
   */
  public static String millisToShortDHMS(long duration) {
    String res = "";
    long days  = TimeUnit.MILLISECONDS.toDays(duration);
    long hours = TimeUnit.MILLISECONDS.toHours(duration)
                   - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
    long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
                     - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
    long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
                   - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
    if (days == 0) {
      res = String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
    else {
      res = String.format("%dd%02d:%02d:%02d", days, hours, minutes, seconds);
    }
    return res;
  }

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]