Java Millisecond Format formatMillis(long timeMillis)

Here you can find the source of formatMillis(long timeMillis)

Description

format Millis

License

Open Source License

Declaration

public static String formatMillis(long timeMillis) 

Method Source Code

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

public class Main {
    public static String formatMillis(long timeMillis) {
        long timeSeconds = timeMillis / 1000;
        long days = timeSeconds / 60 / 60 / 24;
        long hours = timeSeconds / 60 / 60 % 24;
        long mins = timeSeconds / 60 % 60;
        long seconds = timeSeconds % 60;
        StringBuilder sb = new StringBuilder();
        boolean hasDays = days > 0;
        boolean hasHours = hours > 0;
        boolean hasMins = mins > 0;
        boolean hasSeconds = seconds > 0;
        if (!(hasSeconds || hasMins || hasDays || hasHours))
            return "0 seconds";
        if (hasDays) {
            sb.append(days);/*from www . j av a 2s .co  m*/
            sb.append(" day");
            if (days != 1)
                sb.append('s');
        }
        if (hasHours) {
            if (hasDays) {
                if (hasMins || hasSeconds)
                    sb.append(", ");
                else
                    sb.append(" and ");
            }
            sb.append(hours);
            sb.append(" hour");
            if (hours != 1)
                sb.append('s');
        }
        if (hasMins) {
            if (hasDays || hasHours) {
                if (hasSeconds)
                    sb.append(", ");
                else
                    sb.append(" and ");
            }
            sb.append(mins);
            sb.append(" minute");
            if (mins != 1)
                sb.append('s');
        }
        if (hasSeconds) {
            if (hasDays || hasHours || hasMins)
                sb.append(" and ");
            sb.append(seconds);
            sb.append(" second");
            if (seconds != 1)
                sb.append('s');
        }
        return sb.toString();
    }
}

Related

  1. formatMillis(long millis)
  2. formatMillis(long millis)
  3. formatMillis(long t)
  4. formatMillis(long time)
  5. formatMillis(long time, boolean showMillis)
  6. formatMillis(String millisecond, String format)
  7. formatMillisAsNumber(long millis)
  8. formatMillisAsShortHumanReadablePeriod(long millis)
  9. formatMilliseconds (final long ms)