Java Time Format formatTime(long time)

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

Description

Formats time string from seconds.

License

Open Source License

Parameter

Parameter Description
time to format in seconds

Return

time formatted as hh:mm:ss

Declaration

public static String formatTime(long time) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

public class Main {
    /** Formats time string from seconds.
     *//from ww w.j  a va 2s.c o  m
     * @param time to format in seconds
     * @return time formatted as hh:mm:ss
     */
    public static String formatTime(long time) {
        StringBuffer result = new StringBuffer();

        // determine hours and minutes from seconds
        long hour = time / 3600;
        time -= (hour * 3600);
        long min = time / 60;
        time -= (min * 60);
        long sec = time;

        // format to string
        // append hour
        if (hour > 0) {
            result.append(hour).append(":");
            if (min < 10) {
                result.append("0");
            }
        }

        // append min
        result.append(min).append(":");

        // append sec
        if (sec < 10) {
            result.append("0");
        }
        result.append(sec);

        return result.toString();
    }
}

Related

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