Java Time Format formatTimePeriod(long millis)

Here you can find the source of formatTimePeriod(long millis)

Description

format Time Period

License

Apache License

Declaration

public static String formatTimePeriod(long millis) 

Method Source Code

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

public class Main {
    public static String formatTimePeriod(long millis) {
        long seconds = (millis / 1000) % 60;
        long minutes = (millis / (1000 * 60)) % 60;
        long hours = (millis / (1000 * 60 * 60));// %24;
        long onlyMillis = millis % 1000;
        boolean hasMillis = onlyMillis > 0;
        if (hours > 0) {
            String format = "%d:%02d:%02d";
            if (hasMillis) {
                format += ".%03d";
            } else {
                format += ".0";
            }//from   w  w w .  j av a  2 s .  co m
            return String.format(format, hours, minutes, seconds, onlyMillis);
        } else if (minutes > 0) {
            String format = "%d:%02d";
            if (hasMillis) {
                format += ".%03d";
            } else {
                format += ".0";
            }
            return String.format(format, minutes, seconds, onlyMillis);
        } else if (seconds > 0) {
            String format = "%d";
            if (hasMillis) {
                format += ".%03d seconds";
            } else {
                format += ".0 seconds";
            }
            return String.format(format, seconds, onlyMillis);
        } else {
            return String.format("%d milliseconds", onlyMillis);
        }

    }
}

Related

  1. formatTimeLength(long ms)
  2. formatTimeLikeTimer(long time, boolean appendMs)
  3. formatTimeNicely(long millis)
  4. formatTimeOffset(long offset)
  5. formatTimePart2(long number, String unit)
  6. formatTimePeriod(long timePeriod)
  7. formatTimePeriod(long timestamp)
  8. formatTimeSec(long time)
  9. formatTimespan(int timespan)