Java Time Format formatTime(long timeDiff)

Here you can find the source of formatTime(long timeDiff)

Description

Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.

License

Open Source License

Parameter

Parameter Description
timeDiff The time difference to format

Declaration

public static String formatTime(long timeDiff) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w  w.  j av a2  s . c o  m*/
     * 
     * Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.
     * 
     * @param timeDiff
     *            The time difference to format
     */
    public static String formatTime(long timeDiff) {
        StringBuffer buf = new StringBuffer();
        long hours = timeDiff / (60 * 60 * 1000);
        long rem = (timeDiff % (60 * 60 * 1000));
        long minutes = rem / (60 * 1000);
        rem = rem % (60 * 1000);
        long seconds = rem / 1000;

        if (hours != 0) {
            buf.append(hours);
            buf.append("hrs, ");
        }
        if (minutes != 0) {
            buf.append(minutes);
            buf.append("mins, ");
        }
        // return "0sec if no difference
        buf.append(seconds);
        buf.append("sec");
        return buf.toString();
    }

    public static String toString(String[] content, String sign) {
        if (null == content) {
            return null;
        }

        sign = null == sign ? "," : sign;
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < content.length; i++) {
            strBuilder.append(content[i]);
            if (i < content.length - 1) {
                strBuilder.append(sign);
            }
        }

        return strBuilder.toString();
    }
}

Related

  1. formatTime(long time)
  2. formatTime(long time)
  3. formatTime(long time)
  4. formatTime(long time)
  5. formatTime(long time, String syntax, boolean extraZeros)
  6. formatTime(long timeDiff)
  7. formatTime(long timeDiffMillis)
  8. formatTime(long timeInMilli)
  9. formatTime(long timeInSecond)