Java Time Format formatTime(long seconds)

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

Description

Format elapsed time as [[h+]:[mm]]:ss.

License

Open Source License

Declaration

public static String formatTime(long seconds) 

Method Source Code

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

public class Main {
    /** Format elapsed time as [[h+]:[mm]]:ss. */
    public static String formatTime(long seconds) {
        StringBuilder buffer = new StringBuilder(8);
        if (seconds < 0) {
            buffer.append('-');
            seconds *= -1;//from   w  w  w  . j  a va 2 s.c  o  m
        }
        long hours = seconds / 3600;
        if (hours > 0) {
            if (hours > 9999)
                // Extremely large numbers are likely a problem in
                // Date.getTime(), as it can happen when running in the
                // netbeans profiler (version 5.5)
                return "--:--";
            buffer.append(hours);
            buffer.append(':');
        }
        seconds %= 3600;
        long minutes = seconds / 60;
        seconds %= 60;
        if (minutes >= 10)
            buffer.append(minutes);
        else {
            buffer.append('0');
            buffer.append(minutes);
        }
        buffer.append(':');
        if (seconds >= 10)
            buffer.append(seconds);
        else {
            buffer.append('0');
            buffer.append(seconds);
        }
        return buffer.toString();
    }
}

Related

  1. formatTime(long ms)
  2. formatTime(long msElapsed)
  3. formatTime(Long num)
  4. formatTime(long sec)
  5. formatTime(long seconds)
  6. formatTime(long seconds)
  7. formatTime(long time)
  8. formatTime(long time)
  9. formatTime(long time)