Java Second Format formatSeconds(double seconds)

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

Description

Formats seconds in human readable form.

License

Apache License

Parameter

Parameter Description
seconds the seconds

Return

the formatted seconds

Declaration

public static String formatSeconds(double seconds) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**/* w ww.  j a v  a  2 s. co  m*/
     * Formats seconds in human readable form.
     * 
     * @param seconds the seconds
     * @return the formatted seconds
     */
    public static String formatSeconds(double seconds) {
        StringBuilder result = new StringBuilder();
        int minutes = (int) (seconds / 60);
        int digits = 1;

        if (seconds < 0.01) {
            digits = 6;
        } else if (seconds < 1) {
            digits = 3;
        }

        seconds -= minutes * 60;

        if (minutes > 0) {
            result.append(minutes).append(" m ");
        }

        result.append(String.format("%,." + digits + "f s", seconds));

        return result.toString();
    }
}

Related

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