Java Time Format formatTime(int seconds)

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

Description

Formats the time information as a string.

License

Open Source License

Parameter

Parameter Description
seconds The number of seconds.

Return

A formatted string.

Declaration

public static String formatTime(int seconds) 

Method Source Code

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

public class Main {
    /**/*from w  w  w.j  a  v a  2s .c  o m*/
     * Formats the time information as a string.
     * @param seconds The number of seconds.
     * @return A formatted string.
     */
    public static String formatTime(int seconds) {
        StringBuilder result = new StringBuilder();
        int days, hrs, mins, secs;
        days = seconds / (60 * 60 * 24);
        seconds %= 60 * 60 * 24;

        hrs = seconds / (60 * 60);
        seconds %= 60 * 60;

        mins = seconds / 60;
        secs = seconds % 60;

        if (days > 0)
            result.append(days).append("d ");
        if (hrs > 0)
            result.append(hrs).append("h ");
        if (mins > 0)
            result.append(mins).append("m ");
        if (secs > 0)
            result.append(secs).append("s ");

        return result.toString();
    }
}

Related

  1. formatTime(float seconds)
  2. formatTime(int minutes)
  3. formatTime(int minutes)
  4. formatTime(int originalTime)
  5. formatTime(int seconds)
  6. formatTime(int seconds)
  7. formatTime(int secs)
  8. formatTime(int time)
  9. formatTime(int time)