From jquery timeago plugin
This provides you more options and formats than the answers above.
public static Date currentDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
public static String getTimeAgo(Date date, Context ctx) {
if(date == null) {
return null;
}
long time = date.getTime();
Date curDate = currentDate();
long now = curDate.getTime();
if (time > now || time <= 0) {
return null;
}
int dim = getTimeDistanceInMinutes(time);
String timeAgo = null;
if (dim == 0) {
timeAgo = ctx.getResources().getString(R.string.date_util_term_less) + " " + ctx.getResources().getString(R.string.date_util_term_a) + " " + ctx.getResources().getString(R.string.date_util_unit_minute);
} else if (dim == 1) {
return "1 " + ctx.getResources().getString(R.string.date_util_unit_minute);
} else if (dim >= 2 && dim <= 44) {
timeAgo = dim + " " + ctx.getResources().getString(R.string.date_util_unit_minutes);
} else if (dim >= 45 && dim <= 89) {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_about) + " "+ctx.getResources().getString(R.string.date_util_term_an)+ " " + ctx.getResources().getString(R.string.date_util_unit_hour);
} else if (dim >= 90 && dim <= 1439) {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_about) + " " + (Math.round(dim / 60)) + " " + ctx.getResources().getString(R.string.date_util_unit_hours);
} else if (dim >= 1440 && dim <= 2519) {
timeAgo = "1 " + ctx.getResources().getString(R.string.date_util_unit_day);
} else if (dim >= 2520 && dim <= 43199) {
timeAgo = (Math.round(dim / 1440)) + " " + ctx.getResources().getString(R.string.date_util_unit_days);
} else if (dim >= 43200 && dim <= 86399) {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_about) + " "+ctx.getResources().getString(R.string.date_util_term_a)+ " " + ctx.getResources().getString(R.string.date_util_unit_month);
} else if (dim >= 86400 && dim <= 525599) {
timeAgo = (Math.round(dim / 43200)) + " " + ctx.getResources().getString(R.string.date_util_unit_months);
} else if (dim >= 525600 && dim <= 655199) {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_about) + " "+ctx.getResources().getString(R.string.date_util_term_a)+ " " + ctx.getResources().getString(R.string.date_util_unit_year);
} else if (dim >= 655200 && dim <= 914399) {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_over) + " "+ctx.getResources().getString(R.string.date_util_term_a)+ " " + ctx.getResources().getString(R.string.date_util_unit_year);
} else if (dim >= 914400 && dim <= 1051199) {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_almost) + " 2 " + ctx.getResources().getString(R.string.date_util_unit_years);
} else {
timeAgo = ctx.getResources().getString(R.string.date_util_prefix_about) + " " + (Math.round(dim / 525600)) + " " + ctx.getResources().getString(R.string.date_util_unit_years);
}
return timeAgo + " " + ctx.getResources().getString(R.string.date_util_suffix);
}
private static int getTimeDistanceInMinutes(long time) {
long timeDistance = currentDate().getTime() - time;
return Math.round((Math.abs(timeDistance) / 1000) / 60);
}
Add following strings in your string.xml file
less than
a
an
second
seconds
minute
minutes
hour
hours
day
days
month
months
year
years
less than a
about
over
almost
ago