Android Date Interval Get formatDuration(int duration)

Here you can find the source of formatDuration(int duration)

Description

Formats a duration in ms to something more readable.

Parameter

Parameter Description
duration The duration in milliseconds.

Return

"(h:)mm:ss", i.e. "(1:)23:45".

Declaration

public static String formatDuration(int duration) 

Method Source Code

//package com.java2s;

public class Main {
    public static final long ONE_SECOND = 1000;
    public static final long ONE_MINUTE = 60 * ONE_SECOND;
    public static final long ONE_HOUR = 60 * ONE_MINUTE;

    /**/* ww w . ja v  a  2s .  c o m*/
     * Formats a duration in ms to something more readable.
     * Splits the duration in hours, minutes and seconds. 
     * Only shows hours when the duration exceeds an hour.
     *
     * @param duration The duration in milliseconds.
     *
     * @return "(h:)mm:ss", i.e. "(1:)23:45".
     */
    public static String formatDuration(int duration) {
        int hours = (int) (duration / ONE_HOUR);
        duration -= hours * ONE_HOUR;
        int minutes = (int) (duration / ONE_MINUTE);
        duration -= minutes * ONE_MINUTE;
        int seconds = (int) (duration / ONE_SECOND);

        String formattedDuration = "";
        if (hours > 0) {
            formattedDuration = String.format("%d:%02d:%02d", hours,
                    minutes, seconds);
        } else {
            formattedDuration = String
                    .format("%02d:%02d", minutes, seconds);
        }

        return formattedDuration;
    }
}

Related

  1. totalDays(Object o)
  2. calculatorDaysAgo(String date, Locale locale, String format)
  3. getFirstInterval(Context context, long lastupdate, long updateinterval)
  4. timeDifference(Date date)
  5. getTimeRangeStr(Date startDate, Date endDate)
  6. calcTimeBetween(Date start, Date end)
  7. getOffectDay(long date1, long date2)
  8. getOffectDay(long date1, long date2)
  9. getOffectHour(long date1, long date2)