Java Second Convert seconds2time(long seconds)

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

Description

Converts a value in seconds to: "d:hh:mm:ss" where d=days, hh=hours, mm=minutes, ss=seconds, or "h:mm:ss" where h=hours<24, mm=minutes, ss=seconds, or "m:ss" where m=minutes<60, ss=seconds

License

Open Source License

Declaration

public static String seconds2time(long seconds) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .j  a  v a2 s. c  o m*/
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a value in seconds to:
     *     "d:hh:mm:ss" where d=days, hh=hours, mm=minutes, ss=seconds, or
     *     "h:mm:ss" where h=hours<24, mm=minutes, ss=seconds, or
     *     "m:ss" where m=minutes<60, ss=seconds
     */
    public static String seconds2time(long seconds) {
        long minutes = seconds / 60;
        seconds = seconds - minutes * 60;
        long hours = minutes / 60;
        minutes = minutes - hours * 60;
        long days = hours / 24;
        hours = hours - days * 24;
        // build the numbers into a string
        StringBuilder time = new StringBuilder();
        if (days != 0) {
            time.append(Long.toString(days));
            time.append(":");
            if (hours < 10)
                time.append("0");
        }
        if (days != 0 || hours != 0) {
            time.append(Long.toString(hours));
            time.append(":");
            if (minutes < 10)
                time.append("0");
        }
        time.append(Long.toString(minutes));
        time.append(":");
        if (seconds < 10)
            time.append("0");
        time.append(Long.toString(seconds));
        return time.toString();
    }
}

Related

  1. convertToSeconds(String timeStr)
  2. convertToTenthsOfASecond(long epochSeconds, int nanos)
  3. secondConvertToString(final long spentSeconds)
  4. seconds2microseconds(double secs)
  5. seconds2String(long seconds)
  6. seconds2timecents(double seconds)
  7. secondsToCycles(double secs, double hz)
  8. secondsToHHMMSS(int secs)
  9. secondsToHHMMSS(long secs)