Java Integer Format formatInterval(long interval)

Here you can find the source of formatInterval(long interval)

Description

Returns human readable representation of interval value in days, hours etc.

License

Open Source License

Parameter

Parameter Description
interval in seconds.

Return

human readable interval specification.

Declaration

public static String formatInterval(long interval) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   ww w . j a v  a2 s  .  co m
     * Returns human readable representation of interval value in days, hours etc.
     * 
     * @param interval in seconds.
     * @return human readable interval specification.
     */
    public static String formatInterval(long interval) {
        long days = interval / (24 * 60 * 60);
        interval -= days * 24 * 60 * 60;
        long hours = interval / (60 * 60);
        interval -= hours * 60 * 60;
        long minutes = interval / 60;
        interval -= minutes * 60;
        long seconds = interval;
        StringBuffer buff = new StringBuffer();
        if (days > 0) {
            buff.append(days).append(" days, ");
        }
        if (days > 0 || hours > 0) {
            buff.append(hours).append(" hours, ");
        }
        if (days > 0 || hours > 0 || minutes > 0) {
            buff.append(minutes).append(" minutes, ");
        }
        buff.append(seconds).append(" seconds");
        return buff.toString();
    }
}

Related

  1. formatInteger(int value, int length)
  2. formatInteger(Integer value)
  3. formatIntegerBase2WithLeadingZeros(long x, int length)
  4. formatIntegerWithLeadingZeros(long x, int radix, int length)
  5. formatIntents(String[] intents)
  6. formatIntoHHMMSS(int secsIn)
  7. formatIntoHHMMSS(long l)
  8. formatIntOrLong(long v)
  9. formatInts(int[] vals)