Java Time Format formatTime(long time)

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

Description

Format a milisecond number.

License

LGPL

Parameter

Parameter Description
time a parameter

Return

String

Declaration

public static String formatTime(long time) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/* w  ww.j  a  va 2  s  .  c  o  m*/
     * Format a milisecond number.
     * 
     * @param time
     * @return String
     */
    public static String formatTime(long time) {

        String elapsedTime;

        long seconds = time / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;

        long sec_remainder = seconds % 60;
        long min_remainder = minutes % 60;
        long hours_remainder = hours % 24;

        if (minutes < 1) {
            elapsedTime = (seconds <= 9 ? "0" : "") + new Long(seconds).toString() + " sec";
        } else {
            if (hours < 1) {
                elapsedTime = (minutes <= 9 ? "0" : "") + new Long(minutes).toString() + ":"
                        + (sec_remainder <= 9 ? "0" : "") + new Long(sec_remainder).toString() + " min";
            } else {
                elapsedTime = (hours <= 9 ? "0" : "") + new Long(hours).toString() + ":"
                        + (min_remainder <= 9 ? "0" : "") + new Long(min_remainder).toString() + ":"
                        + (sec_remainder <= 9 ? "0" : "") + new Long(sec_remainder).toString();
            }
        }
        return elapsedTime;
    }
}

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)
  8. formatTime(long time, String syntax, boolean extraZeros)
  9. formatTime(long timeDiff)