Java Duration Format formatDuration(int seconds)

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

Description

format Duration

License

Open Source License

Declaration

public static String formatDuration(int seconds) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String formatDuration(int seconds) {
        if (seconds == 0) {
            return "";
        }/*w ww. j a v a  2 s . c  o m*/

        int days = seconds / 86400;
        int hours = (seconds % 86400) / 3600;
        int mins = (seconds % 3600) / 60;
        int secs = seconds % 60;

        StringBuffer buf = new StringBuffer();
        if (days > 0) {
            buf.append(days).append("d ");
        }
        if (hours > 0) {
            buf.append(hours).append("h ");
        }
        if (mins > 0) {
            buf.append(mins).append("m ");
        }
        if (secs > 0) {
            buf.append(secs).append("s ");
        }
        buf.setLength(buf.length() - 1); // trim
        return buf.toString();
    }
}

Related

  1. formatDuration(final Duration duration, final boolean wrapInDurationFunction)
  2. formatDuration(final int duration)
  3. formatDuration(final long milliSeconds)
  4. formatDuration(final long seconds)
  5. formatDuration(int durationMins)
  6. formatDuration(int seconds)
  7. formatDuration(long duration)
  8. formatDuration(long duration)
  9. formatDuration(long duration)