azriebakri
azriebakri

Reputation: 1151

DateUtils.getRelativeTimeSpanString not giving proper result

Currently I'm using DateUtils.getRelativeTimeSpanString to get an output like.

8 minutes ago
8 hours ago

But instead it return. My code is exactly in this post

In 4 hr
In 5 hr

Here is my code

long postTime = getDateInMillis(pubDate);
CharSequence time = DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(),postTime,DateUtils.MINUTE_IN_MILLIS,DateUtils.FORMAT_ABBREV_RELATIVE);

My getDateInMillis method

 public static long getDateInMillis(String srcDate) {

    SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",Locale.getDefault());

    long dateInMillis = 0;
    try {
        Date date = formatter.parse(srcDate);
        dateInMillis = date.getTime();
        return dateInMillis;
    }

    catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    return 0;
}

Please help me getting the right format that I want. Thanks.

Upvotes: 0

Views: 597

Answers (1)

azriebakri
azriebakri

Reputation: 1151

My silly mistake.

The reason why it happen was due to this line

CharSequence time = DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(),postTime,DateUtils.MINUTE_IN_MILLIS,DateUtils.FORMAT_ABBREV_RELATIVE);

I switched the position of System.currentTimeMillis(), and postTime then it returned the right format.

Upvotes: 1

Related Questions