Java Time Format formatTime(long millis, int precision)

Here you can find the source of formatTime(long millis, int precision)

Description

format Time

License

Open Source License

Declaration

public static String formatTime(long millis, int precision) 

Method Source Code

//package com.java2s;

public class Main {
    private static final String[] timeFormat = new String[] { "d ", "h ", "m ", "s ", "ms" };

    public static String formatTime(long millis, int precision) {
        long[] la = new long[5];
        la[0] = (millis / 86400000);// days
        la[1] = (millis / 3600000) % 24;// hours
        la[2] = (millis / 60000) % 60;// minutes
        la[3] = (millis / 1000) % 60;// seconds
        la[4] = (millis % 1000);// ms

        int index = 0;
        for (int i = 0; i < la.length; i++) {
            if (la[i] != 0) {
                index = i;/*from w w  w. ja  v a2s. c o  m*/
                break;
            }
        }

        StringBuilder buf = new StringBuilder();
        int validLength = la.length - index;
        for (int i = 0; (i < validLength && i < precision); i++) {
            buf.append(la[index]).append(timeFormat[index]);
            index++;
        }
        return buf.toString();
    }
}

Related

  1. formatTime(long elapsedTime)
  2. formatTime(long millis)
  3. formatTime(long millis)
  4. formatTime(long millis)
  5. formatTime(long millis)
  6. formatTime(long ms)
  7. formatTime(long ms)
  8. formatTime(long msElapsed)
  9. formatTime(Long num)