Now i am going on with the Android Time Ago i am getting days ago but not getting weeks ago
Here i tried:
CharSequence CS = DateUtils.getRelativeTimeSpanString(DateUtils.MINUTE_IN_MILLIS, now.getTime(),
DateUtils.WEEK_IN_MILLIS, 0);
Exactly what i need is if it less than week should show these much days remaning.
if a week and above shows how many weeks and reaches a month should shows a month ago or two month ago like goes on
How can i get this can anyone help me.
You can not do that via existing methods of DateUtils
. You can use this implementation for that, but keep in mind that it works correct only for past (weeks, month, years). If you want to handle future then you need care about that use case.
public static final long AVERAGE_MONTH_IN_MILLIS = DateUtils.DAY_IN_MILLIS * 30;
private String getRelationTime(long time) {
final long now = new Date().getTime();
final long delta = now - time;
long resolution;
if (delta <= DateUtils.MINUTE_IN_MILLIS) {
resolution = DateUtils.SECOND_IN_MILLIS;
} else if (delta <= DateUtils.HOUR_IN_MILLIS) {
resolution = DateUtils.MINUTE_IN_MILLIS;
} else if (delta <= DateUtils.DAY_IN_MILLIS) {
resolution = DateUtils.HOUR_IN_MILLIS;
} else if (delta <= DateUtils.WEEK_IN_MILLIS) {
resolution = DateUtils.DAY_IN_MILLIS;
} else if (delta <= AVERAGE_MONTH_IN_MILLIS) {
return Integer.toString((int) (delta / DateUtils.WEEK_IN_MILLIS)) + " weeks(s) ago";
} else if (delta <= DateUtils.YEAR_IN_MILLIS) {
return Integer.toString((int) (delta / AVERAGE_MONTH_IN_MILLIS)) + " month(s) ago";
} else {
return Integer.toString((int) (delta / DateUtils.YEAR_IN_MILLIS)) + " year(s) ago";
}
return DateUtils.getRelativeTimeSpanString(time, now, resolution).toString();
}