Android examples for java.util:Date Algorithm
Get time string since a specified date
import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ /**/*from w w w . j av a2 s . c om*/ * Get time string since a specified date */ public static String getTimeSince(final Date itemTime) { final Date currentTime = new Date(); final long ms = currentTime.getTime() - itemTime.getTime(); final long sec = ms / 1000; final long min = sec / 60; final long hour = min / 60; final long day = hour / 24; final long month = day / 30; final long year = day / 365; if (year > 0) { if (year == 1) { return year + " year"; } else { return year + " years"; } } else if (month > 0) { if (month == 1) { return month + " month"; } else { return month + " months"; } } else if (day > 0) { if (day == 1) { return day + " day"; } else { return day + " days"; } } else if (hour > 0) { if (hour == 1) { return hour + " hour"; } else { return hour + " hours"; } } else if (min > 0) { if (min == 1) { return min + " minute"; } else { return min + " minutes"; } } else if (sec > 0) { if (sec == 1) { return sec + " second"; } else { return sec + " seconds"; } } else { return ms + " ms"; } } }