Java Time Readable Format humanTime(long start, long end)

Here you can find the source of humanTime(long start, long end)

Description

Construct a human readable string of a duration

License

Open Source License

Parameter

Parameter Description
start Beginning time in seconds.
end End time in seconds

Return

A human readable string of a duration

Declaration

public static String humanTime(long start, long end) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from ww  w. j  a v a2  s.c  o m
     * Construct a human readable string of a duration
     * @param start Beginning time in seconds.
     * @param end End time in seconds
     * @return A human readable string of a duration
     */
    public static String humanTime(long start, long end) {
        if (start != -1L) {
            final long finalTime = (end - start) / 1000L;
            final long MINUTE = 60L;
            final long HOUR = 60 * MINUTE;
            final long DAY = 24 * HOUR;
            final long MONTH = 30 * DAY;
            final long YEAR = 365 * DAY;

            if (finalTime >= YEAR) {
                String s = (finalTime >= (2 * YEAR)) ? "years" : "year";
                return (finalTime / YEAR + " " + s);
            } else if (finalTime >= MONTH) {
                String s = (finalTime >= (2 * MONTH)) ? "months" : "month";
                return (finalTime / MONTH + " " + s);
            } else if (finalTime >= DAY) {
                String s = (finalTime >= (2 * DAY)) ? "days" : "day";
                return (finalTime / DAY + " " + s);
            } else if (finalTime >= HOUR) {
                String s = (finalTime >= (2 * HOUR)) ? "hours" : "hour";
                return (finalTime / HOUR + " " + s);
            } else if (finalTime >= MINUTE) {
                String s = (finalTime >= (2 * MINUTE)) ? "minutes" : "minute";
                return (finalTime / MINUTE + " " + s);
            } else {
                String s = (finalTime >= 2) ? "seconds" : "second";
                return (finalTime + " " + s);
            }
        }
        return null;
    }
}

Related

  1. getMaxTimeByStringDate(String date)
  2. humanTime(long ms)
  3. humanTime(long seconds)
  4. toHumanable(long time_millsecod)
  5. toTime(int duration)
  6. toTime(long ms)
  7. toTime(long nanos)