Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android getRelativeTimeSpanString() bug?

using the built in getRelativeTimeSpanString() method in android DateUtil I am unable to get the "minutes ago" "days ago" etc. time elapsed response that the documentation says that I should receive, instead I get a result that just displays the actual date in a way such as "may 12 2010", not sure if this is a bug or what but Ive tried both getRelativeTimeSpanString (long time, long now, long minResolution) and getRelativeTimeSpanString (long startTime), both just return the actual date of the variable "long time"

here is my code

private void setJoineddate() {
    Date currentDate = new Date();
    long currentDateLong = currentDate.getTime();
    long oldDate = join_date.getTime();

    CharSequence relativeTime = DateUtils
                         .getRelativeTimeSpanString(oldDate, currentDateLong, 0);
    joindate.setText(relativeTime);

}
like image 469
Edmund Rojas Avatar asked Dec 31 '11 12:12

Edmund Rojas


2 Answers

Not a bug. getRelativeTimeSpanString will only give you a relative time string for up to one week in time difference. So if you use DateUtils.getRelativeTimeSpanString(time2, now, 0L, DateUtils.FORMAT_ABBREV_RELATIVE) with the following values

long now = System.currentTimeMillis();
long time2 = now - DateUtils.WEEK_IN_MILLIS;

you'll get 1 October if today is 8 October, but

long now = System.currentTimeMillis();
long time2 = now - DateUtils.WEEK_IN_MILLIS + 1;

will give you 7 days ago.

like image 147
vikki Avatar answered Sep 22 '22 14:09

vikki


Try this code

DateUtils.getRelativeTimeSpanString(oldDate, currentDateLong,
    0L, DateUtils.FORMAT_ABBREV_ALL);

It will return Just now, 3 minutes ago and so on. Read documentation and play with third parameter to modify strings returned by the function.

like image 42
StenaviN Avatar answered Sep 23 '22 14:09

StenaviN