Java Duration Format formatDuration(long seconds)

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

Description

Formats a duration in seconds as a string of the form "3d 04:12:56".

License

Open Source License

Parameter

Parameter Description
seconds A duration in seconds

Return

a formatted string of the form "3d 04:12:56"

Declaration

public static String formatDuration(long seconds) 

Method Source Code

//package com.java2s;
/*/* w w w. j a  va 2s  .c o m*/
 * #%L
 * Netarchivesuite - common
 * %%
 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
 *             the National Library of France and the Austrian National Library.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

public class Main {
    /** A day in seconds. */
    private static final long DAY = 60 * 60 * 24;
    /** An hour in seconds. */
    private static final long HOUR = 60 * 60;
    /** A minute in seconds. */
    private static final long MINUTE = 60;

    /**
     * Formats a duration in seconds as a string of the form "3d 04:12:56".
     *
     * @param seconds A duration in seconds
     * @return a formatted string of the form "3d 04:12:56"
     */
    public static String formatDuration(long seconds) {
        if (seconds > 0L) {
            long lRest;

            String strDays = formatDurationLpad(String.valueOf(seconds / DAY)) + "d ";
            lRest = seconds % DAY;

            String strHours = formatDurationLpad(String.valueOf(lRest / HOUR)) + ":";
            lRest %= HOUR;

            String strMinutes = formatDurationLpad(String.valueOf(lRest / MINUTE)) + ":";
            lRest %= MINUTE;

            String strSeconds = formatDurationLpad(String.valueOf(lRest));

            return strDays + strHours + strMinutes + strSeconds;

        } else if (seconds == 0L) {
            return "0d 00:00:00";
        } else {
            return "-1";
        }
    }

    /**
     * Leftpad the string with "0", if the string is only one character long.
     *
     * @param s The given string
     * @return Return a string leftpadded with a "0" if the string is only one character long, Otherwise just return the
     * string.
     */
    private static String formatDurationLpad(final String s) {
        return (s.length() == 1 ? "0" + s : s);
    }
}

Related

  1. formatDuration(long milliseconds)
  2. formatDuration(long milliseconds)
  3. formatDuration(long ms)
  4. formatDuration(long ms)
  5. formatDuration(long number)
  6. formatDuration(long seconds)
  7. formatDuration(long t_millis)
  8. formatDurationAsTime(final int duration)
  9. formatDurationHMSms(long ms)