Java Duration Format formatWordyDuration(long milliseconds)

Here you can find the source of formatWordyDuration(long milliseconds)

Description

Format a wordy time duration (e.g., "5 days, 3 hours, 23 minutes").

License

Open Source License

Parameter

Parameter Description
milliseconds Time duration in milliseconds

Return

Formated time duration.

Declaration


public static String formatWordyDuration(long milliseconds) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /**   Format a wordy time duration (e.g., "5 days, 3 hours, 23 minutes").
     */*from   ww w  . j av a  2  s  .  c o  m*/
     *   @param   milliseconds   Time duration in milliseconds
     *
     *   @return               Formated time duration.
     */

    public static String formatWordyDuration(long milliseconds) {
        long seconds = milliseconds / 1000;
        long days = seconds / 86400;
        seconds -= days * 86400;
        long hours = seconds / 3600;
        seconds -= hours * 3600;
        long minutes = seconds / 60;
        seconds -= minutes * 60;
        StringBuffer buf = new StringBuffer();
        if (days > 0)
            buf.append(days + (days == 1 ? " day, " : " days, "));
        if (days > 0 || hours > 0)
            buf.append(hours + (hours == 1 ? " hour, " : " hours, "));
        buf.append(minutes + (minutes == 1 ? " minute" : " minutes"));
        return buf.toString();
    }
}

Related

  1. formattedStringToDuration(final String formattedDuration)
  2. formatTime(long duration)
  3. formatTime(long duration)
  4. formatTimeDuration(long timeDuration)
  5. formatTS(long duration)
  6. PrintDuration(long NanoSeconds)
  7. PrintDurationConciseFromMs(long MilliSeconds)
  8. PrintDurationMilliSeconds(long NanoSeconds)