Java Time Readable Format toTimeSpanDescription(long time)

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

Description

to Time Span Description

License

Open Source License

Parameter

Parameter Description
time The time span in milliseconds.

Return

A human readable description of the time span.

Declaration

public static String toTimeSpanDescription(long time) 

Method Source Code

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

public class Main {
    public static final long SECOND_MS = 1000;
    public static final long MINUTE_MS = SECOND_MS * 60;
    public static final long HOUR_MS = MINUTE_MS * 60;
    public static final long DAY_MS = HOUR_MS * 24;

    /**//ww  w .ja  va2  s  .c  o  m
     * @param time The time span in milliseconds.
     *
     * @return A human readable description of the time span.
     */
    public static String toTimeSpanDescription(long time) {
        int days = (int) (time / DAY_MS);
        time -= days * DAY_MS;
        int hours = (int) (time / HOUR_MS);
        time -= hours * HOUR_MS;
        int minutes = (int) (time / MINUTE_MS);
        time -= minutes * MINUTE_MS;
        int seconds = (int) (time / SECOND_MS);
        time -= seconds * SECOND_MS;
        int ms = (int) time;
        if (days != 0) {
            return days + " days and " + hours + " hours";
        } else if (hours != 0) {
            return hours + " hours and " + minutes + " minutes";
        } else if (minutes != 0) {
            return minutes + " minutes and " + seconds + " seconds";
        } else if (seconds != 0) {
            return seconds + " seconds";
        } else {
            return ms + " ms.";
        }
    }
}

Related

  1. toTime(long time)
  2. toTime2(int value, int nanos, int meta)
  3. toTimeExpression(long ms, int frames)
  4. toTimeFormat(String _srcTime)
  5. toTimeHumanReadable(final long time)
  6. toTimestamp(long time)
  7. toTimeStampString(String timeStr)
  8. toTimeString(double millis)
  9. toTimeString(long frame)