Java Second Convert convertSecondsToTime(int seconds)

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

Description

Converts the given seconds to a time format with following format: days:hours:minutes:seconds.

License

Open Source License

Declaration

public static String convertSecondsToTime(int seconds) 

Method Source Code

//package com.java2s;
/*/* w w  w.ja va 2 s.c o m*/
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2011 Phex Development Group
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  --- SVN Information ---
 *  $Id: TimeFormatUtils.java 4526 2011-06-28 17:25:36Z gregork $
 */

public class Main {
    /**
     * Converts the given seconds to a time format with following format:
     * days:hours:minutes:seconds. When days < 0 the format will be
     * hours:minutes:seconds. When hours < 0 the format will be minutes:seconds.
     * Values < 10 will be padded with a 0.
     */
    public static String convertSecondsToTime(int seconds) {
        StringBuffer buffer = new StringBuffer();
        int days = seconds / 86400;
        int hours = (seconds / 3600) % 24;
        int minutes = (seconds / 60) % 60;
        int secs = seconds % 60;

        if (days > 0) // Display days and hours
        {
            buffer.append(Integer.toString(days));
            buffer.append(':');
            if (hours < 10) {
                buffer.append('0');
            }
        }
        if (days > 0 || hours > 0) {
            buffer.append(Integer.toString(hours));
            buffer.append(':');
            if (minutes < 10) {
                buffer.append('0');
            }
        }

        buffer.append(Integer.toString(minutes));
        buffer.append(':');
        if (secs < 10) {
            buffer.append('0');
        }
        buffer.append(Integer.toString(secs));
        return buffer.toString();
    }
}

Related

  1. convertSecondsToDays(double secs)
  2. convertSecondsToMiliseconds(double seconds)
  3. convertSecondsToMiliseconds(int secondsToConvert)
  4. convertSecondsToMillies(final long seconds)
  5. convertSecondsToString(double seconds)
  6. convertSecondsToTime(int totalSecs)
  7. convertSecondsToTimecode(float totalSeconds)
  8. convertSecondsToTimeUnits(int seconds)
  9. convertTimecodeToSeconds(String timecode)