package com.devbrackets.android.exomedia.util; import android.text.format.DateUtils; import java.util.Formatter; import java.util.Locale; public class TimeFormatUtil { private static StringBuilder formatBuilder = new StringBuilder(); private static Formatter formatter = new Formatter(formatBuilder, Locale.getDefault()); /** * Formats the specified milliseconds to a human readable format * in the form of (Hours : Minutes : Seconds). If the specified * milliseconds is less than 0 the resulting format will be * "--:--" to represent an unknown time * * @param milliseconds The time in milliseconds to format * @return The human readable time */ public static String formatMs(long milliseconds) { if (milliseconds < 0) { return "--:--"; } long seconds = (milliseconds % DateUtils.MINUTE_IN_MILLIS) / DateUtils.SECOND_IN_MILLIS; long minutes = (milliseconds % DateUtils.HOUR_IN_MILLIS) / DateUtils.MINUTE_IN_MILLIS; long hours = (milliseconds % DateUtils.DAY_IN_MILLIS) / DateUtils.HOUR_IN_MILLIS; formatBuilder.setLength(0); if (hours > 0) { return formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString(); } return formatter.format("%02d:%02d", minutes, seconds).toString(); } }