/** * Generates a human representation of distance in time between two time stamps. This could take a form: "less than * a minute", or "about a year". * * @param fromTime start timestamp. This is a representation of time in milliseconds from January 1 1970. * @param toTime end timestamp. This is a representation of time in milliseconds from January 1 1970. * @return human representation if distance in time between <code>fromTime</code> and <code>toTime</code>. * */ public static String toHumanFormat(long fromTime, long toTime) { if(! (toTime >= fromTime)) { throw new IllegalArgumentException("toTime must be >= fromTime"); } long distanceInSeconds = (toTime - fromTime) / 1000; long distanceInMinutes = distanceInSeconds / 60; if (distanceInMinutes == 0) { return "less than a minute"; } if (distanceInMinutes == 1) { return "a minute"; } if(inRange(2, 44, distanceInMinutes)) { return distanceInMinutes + " minutes"; } if(inRange(45, 89, distanceInMinutes)) { return "about 1 hour"; } if(inRange(90, 1439, distanceInMinutes)) { return "about " + Math.round(distanceInMinutes/60f) + " hours" ; } if(inRange(1440, 2879, distanceInMinutes)) { return "1 day"; } if(inRange(2880, 43199, distanceInMinutes)) { return Math.round(distanceInMinutes/1440f) + " days"; } if(inRange(43200, 86399, distanceInMinutes)) { return "about 1 month"; } if(inRange(86400, 525599, distanceInMinutes)) { return Math.round(distanceInMinutes/43200f) + " months"; } if(inRange(525600, 1051199, distanceInMinutes)) { return "about 1 year"; } return "about " + Math.round(distanceInMinutes/525600f) + " years"; }
/** * This is a convenience method in addition to {@link #toHumanFormat(long, long)}, except the second parameter is * always now. * * @param fromTime start date. This is a representation of time in milliseconds from January 1 1970. * @return human imprecise representation of time difference between the <code>fromTime</code> and now. */ public static String toHumanFormat(long fromTime) { return toHumanFormat(fromTime, new Date().getTime()); }
/** * This is a convenience method in addition to {@link #toHumanFormat(long, long)}, except the second parameter is * always now. * * @param fromTime start date. This is a representation of time in milliseconds from January 1 1970. * @return human imprecise representation of time difference between the <code>fromTime</code> and now. */ public static String toHumanFormat(long fromTime) { return toHumanFormat(fromTime, new Date().getTime()); }
/** * Generates a human representation of distance in time between two time stamps. This could take a form: "less than * a minute", or "about a year". * * @param fromTime start timestamp. This is a representation of time in milliseconds from January 1 1970. * @param toTime end timestamp. This is a representation of time in milliseconds from January 1 1970. * @return human representation if distance in time between <code>fromTime</code> and <code>toTime</code>. * */ public static String toHumanFormat(long fromTime, long toTime) { if(! (toTime >= fromTime)) { throw new IllegalArgumentException("toTime must be >= fromTime"); } long distanceInSeconds = (toTime - fromTime) / 1000; long distanceInMinutes = distanceInSeconds / 60; if (distanceInMinutes == 0) { return "less than a minute"; } if (distanceInMinutes == 1) { return "a minute"; } if(inRange(2, 44, distanceInMinutes)) { return distanceInMinutes + " minutes"; } if(inRange(45, 89, distanceInMinutes)) { return "about 1 hour"; } if(inRange(90, 1439, distanceInMinutes)) { return "about " + Math.round(distanceInMinutes/60f) + " hours" ; } if(inRange(1440, 2879, distanceInMinutes)) { return "1 day"; } if(inRange(2880, 43199, distanceInMinutes)) { return Math.round(distanceInMinutes/1440f) + " days"; } if(inRange(43200, 86399, distanceInMinutes)) { return "about 1 month"; } if(inRange(86400, 525599, distanceInMinutes)) { return Math.round(distanceInMinutes/43200f) + " months"; } if(inRange(525600, 1051199, distanceInMinutes)) { return "about 1 year"; } return "about " + Math.round(distanceInMinutes/525600f) + " years"; }
/** * This is a convenience method in addition to {@link #toHumanFormat(long, long)}, except the second parameter is * always now. * * @param fromTime start date. This is a representation of time in milliseconds from January 1 1970. * @return human imprecise representation of time difference between the <code>fromTime</code> and now. */ public static String toHumanFormat(long fromTime) { return toHumanFormat(fromTime, new Date().getTime()); }
/** * Generates a human representation of distance in time between two time stamps. This could take a form: "less than * a minute", or "about a year". * * @param fromTime start timestamp. This is a representation of time in milliseconds from January 1 1970. * @param toTime end timestamp. This is a representation of time in milliseconds from January 1 1970. * @return human representation if distance in time between <code>fromTime</code> and <code>toTime</code>. * */ public static String toHumanFormat(long fromTime, long toTime) { if(! (toTime >= fromTime)) { throw new IllegalArgumentException("toTime must be >= fromTime"); } long distanceInSeconds = Math.round((toTime - fromTime) / 1000); long distanceInMinutes = Math.round(distanceInSeconds / 60); if (distanceInMinutes == 0) { return "less than a minute"; } if (distanceInMinutes == 1) { return "a minute"; } if(inRange(2, 44, distanceInMinutes)) { return distanceInMinutes + " minutes"; } if(inRange(45, 89, distanceInMinutes)) { return "about 1 hour"; } if(inRange(90, 1439, distanceInMinutes)) { return "about " + Math.round(distanceInMinutes/60f) + " hours" ; } if(inRange(1440, 2879, distanceInMinutes)) { return "1 day"; } if(inRange(2880, 43199, distanceInMinutes)) { return Math.round(distanceInMinutes/1440f) + " days"; } if(inRange(43200, 86399, distanceInMinutes)) { return "about 1 month"; } if(inRange(86400, 525599, distanceInMinutes)) { return Math.round(distanceInMinutes/43200f) + " months"; } if(inRange(525600, 1051199, distanceInMinutes)) { return "about 1 year"; } return "about " + Math.round(distanceInMinutes/525600f) + " years"; }