0

I'm trying to convert a timestamp coming from a JSON API to a relative time span string like this:

try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
                Date date = sdf.parse(item.getTimeStamp());
                long milliseconds = date.getTime();

                CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                        milliseconds,
                        System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
                timestamp.setText(timeAgo);

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

The timestamp comes back in JSON like this: 2014-07-01T00:05:20Z

I'm throwing the exception for Unparseable date

What am I doing wrong here?

jmj
  • 232,312
  • 42
  • 391
  • 431
settheline
  • 3,273
  • 8
  • 30
  • 65

1 Answers1

3

Z expects timezone value, change your pattern to and you don't need SSS since you don't have milliseconds in input

yyyy-MM-dd'T'HH:mm:ss'Z'
jmj
  • 232,312
  • 42
  • 391
  • 431