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);
}
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With