Java Second Format formatSeconds(double seconds)

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

Description

format Seconds

License

Open Source License

Declaration

public static String formatSeconds(double seconds) 

Method Source Code

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

public class Main {
    public static String formatSeconds(double seconds) {
        /*//w w  w  .j  a v a 2  s.  c om
        return formatDuration(Duration.ofSeconds((long) seconds,
        Math.round((seconds - (int) seconds) * 10) * 100000000L));// ,
                                                 // 1);
         */
        int i = (int) seconds;
        int s = i % 60;
        int m = i / 60;
        int min = m % 60;
        int h = m / 60;
        return (h > 0 ? fillZeros(h, 2) + ":" : "") + (m > 0 || h > 0 ? fillZeros(min, 2) + ":" : "")
                + fillZeros(s, 2) + "." + (seconds % 1 != 0 ? String.valueOf(seconds % 1).substring(2, 3) : "0");
    }

    private static String fillZeros(int number, int amount) {

        String s = "";
        if (number == 0) {
            for (int i = 0; i < amount - 1; i++) {
                s += "0";
            }
        } else {
            for (int i = number; i < Math.pow(10, amount - 1); i *= 10) {
                s += "0";
            }
        }
        return s + number;
    }
}

Related

  1. formatMiliSeconds(long time)
  2. formatMilliIntervalAsSeconds(long interval)
  3. formatMinutesSeconds(final long sourceDuration, final TimeUnit sourceUnit)
  4. formatNanoseconds (final long ns)
  5. formatSecondFractions(long numUnits, int denominator)
  6. formatSeconds(double seconds)
  7. formatSeconds(double seconds, int precision)
  8. formatSeconds(double value)
  9. formatSeconds(final int total)