Java Second Format getDHMS(long totalSecond)

Here you can find the source of getDHMS(long totalSecond)

Description

Get a String in HH:MM:SS format for a gievn total seconds (float) total seconds = hour*3600 + minute*60 +seconds, Michael Nguyen

License

Open Source License

Parameter

Parameter Description
totalSecond of seconds

Return

String

Declaration

public static String getDHMS(long totalSecond) 

Method Source Code

//package com.java2s;
/*//from www . j  a v a 2  s. c o m
 * License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt
 */

import java.text.DecimalFormat;

public class Main {
    /**
     *  Get a String in HH:MM:SS format for a gievn total seconds (float)
     *  total seconds = hour*3600 + minute*60 +seconds,  Michael Nguyen
     *  @param totalSecond of seconds
     *  @return String
     */
    public static String getDHMS(long totalSecond) {
        String time = "00:00:00";

        if (totalSecond > 0) {
            String dayStr = "";
            long days = totalSecond / (3600 * 24);
            if (days > 0) {
                dayStr = days + " days ";
                totalSecond -= (3600 * 24);
            }

            DecimalFormat decimalFormat = new DecimalFormat("00");
            long hour = totalSecond / 3600;
            if (hour > 0)
                time = decimalFormat.format(hour);
            else
                time = "00";
            time += ":";
            long minute = (totalSecond - (hour * 3600)) / 60;
            if (minute > 0)
                time += decimalFormat.format(minute);
            else
                time += "00";
            time += ":";
            long second = totalSecond - (hour * 3600) - (minute * 60);
            if (second > 0)
                time += decimalFormat.format(second);
            else
                time += "00";
            return dayStr + time;
        } else {
            return time;
        }

    }
}

Related

  1. formatTime(float seconds)
  2. formatTimeInSeconds(int v)
  3. formatTimeSecondsSinceEpoch(final long time, StringBuilder sbuffer)
  4. formatTimeSpanSeconds(long span)
  5. formatTimeWithOutSeconds(java.util.Date d)
  6. getElapsedSeconds(long startTime)
  7. getFormatMSecond(long long_)
  8. getFormattedDuration(int simTimeSeconds)
  9. getFullDateByMisSecond(long misSecond)