Java Time Format formatTimeString(long millisecond)

Here you can find the source of formatTimeString(long millisecond)

Description

Milliseconds -> hh:mm:ss

License

Open Source License

Declaration

public static String formatTimeString(long millisecond) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  ww  . j  av  a2 s.c  o  m*/
     * Milliseconds -> hh:mm:ss
     */
    public static String formatTimeString(long millisecond) {
        millisecond = millisecond < 0 ? -millisecond : millisecond;

        //      dateFormat.setTimeZone(TimeZone.getTimeZone("GMT + 0")); //BUG: less than 24 hours
        //      return dateFormat.format(millisecond);

        int seconds = (int) (millisecond / 1000);

        StringBuffer result = new StringBuffer("");
        int ss = seconds % 60;
        int mm = seconds / 60 % 60;
        int hh = seconds / 3600;

        if (hh == 0)
            result.append("00");
        else if (hh < 10)
            result.append("0").append(hh);
        else
            result.append(hh);

        result.append(":");

        if (mm == 0)
            result.append("00");
        else if (mm < 10)
            result.append("0").append(mm);
        else
            result.append(mm);

        result.append(":");

        if (ss == 0)
            result.append("00");
        else if (ss < 10)
            result.append("0").append(ss);
        else
            result.append(ss);

        return result.toString();
    }
}

Related

  1. formatTimestampEnd(String timestamp)
  2. formatTimestampForFilename(final long timestamp)
  3. formatTimestampForLogging(final long rawNanosTimestamp)
  4. formatTimestampStart(String timestamp)
  5. formatTimeStep(Integer numberOfTimestepsPerYear, int stepNumber)
  6. formatTimeString(String time)
  7. formatTimeTakenNs(long startTimeNs, String message)
  8. formatTimeToString(long milisec)
  9. formatTimeToString(long seconds)