Here you can find the source of humanTime(long start, long end)
Parameter | Description |
---|---|
start | Beginning time in seconds. |
end | End time in seconds |
public static String humanTime(long start, long end)
//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; } }