Java Second Format secondsToDHMSString(double seconds)

Here you can find the source of secondsToDHMSString(double seconds)

Description

seconds To DHMS String

License

Open Source License

Declaration

public static String secondsToDHMSString(double seconds) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;

public class Main {
    public static String secondsToDHMSString(double seconds) {
        if (seconds < 60) {
            return doubleToString(seconds, 2, 2) + 's';
        }/*w w  w  .j  a  v a2 s  .c  o  m*/
        long secs = (int) (seconds);
        long mins = secs / 60;
        long hours = mins / 60;
        long days = hours / 24;
        secs %= 60;
        mins %= 60;
        hours %= 24;
        StringBuilder result = new StringBuilder();
        if (days > 0) {
            result.append(days);
            result.append('d');
        }
        if ((hours > 0) || (days > 0)) {
            result.append(hours);
            result.append('h');
        }
        if ((hours > 0) || (days > 0) || (mins > 0)) {
            result.append(mins);
            result.append('m');
        }
        result.append(secs);
        result.append('s');
        return result.toString();
    }

    public static String doubleToString(double value, int fractionDigits) {
        return doubleToString(value, 0, fractionDigits);
    }

    public static String doubleToString(double value, int minFractionDigits, int maxFractionDigits) {
        DecimalFormat numberFormat = new DecimalFormat();
        numberFormat.setMinimumFractionDigits(minFractionDigits);
        numberFormat.setMaximumFractionDigits(maxFractionDigits);
        return numberFormat.format(value);
    }
}

Related

  1. parseNoSecondFormat(String sDate)
  2. printElapsedSeconds(long start)
  3. removeSecondsFromDateString(String sdate)
  4. Second2DateString(int v)
  5. secondsBetween(String from, String to, String format)
  6. secondstoHM(long sec)
  7. secondsToHumanDate(long seconds)
  8. secondsToString(long seconds)
  9. secondsToStringFormat(long seconds)