Java Second Get getSecondsInDDHHMMSS(int s)

Here you can find the source of getSecondsInDDHHMMSS(int s)

Description

get Seconds In DDHHMMSS

License

Open Source License

Declaration

public static String getSecondsInDDHHMMSS(int s) 

Method Source Code

//package com.java2s;
/*/* w w w .j a  va  2s. co  m*/
 * Created by Angel Leon (@gubatron), Alden Torres (aldenml)
 * Copyright (c) 2011-2014, FrostWire(R). All rights reserved.
 *
 * 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 {
    public static String getSecondsInDDHHMMSS(int s) {
        if (s < 0) {
            s = 0;
        }

        StringBuilder result = new StringBuilder();

        String DD = "";
        String HH = "";
        String MM = "";
        String SS = "";

        //math
        int days = s / 86400;
        int r = s % 86400;

        int hours = r / 3600;
        r = s % 3600;
        int minutes = r / 60;
        int seconds = r % 60;

        //padding
        DD = String.valueOf(days);
        HH = (hours < 10) ? "0" + hours : String.valueOf(hours);
        MM = (minutes < 10) ? "0" + minutes : String.valueOf(minutes);
        SS = (seconds < 10) ? "0" + seconds : String.valueOf(seconds);

        //lazy formatting
        if (days > 0) {
            result.append(DD);
            result.append(" day");
            if (days > 1) {
                result.append("s");
            }
            return result.toString();
        }

        if (hours > 0) {
            result.append(HH);
            result.append(":");
        }

        result.append(MM);
        result.append(":");
        result.append(SS);

        return result.toString();
    }
}

Related

  1. getSecondsFromHMS(String hmsvalue)
  2. getSecondsFromString(String s)
  3. getSecondsFromString(String text)
  4. getSecondsFromStringDuration(String duration)
  5. getSecondSimpleType(String completeType)
  6. getSecondsText(long seconds)
  7. getSecondsToHHMMSS(double seconds)
  8. getSecondsToMMSS(double seconds)
  9. getSecondTime()