0

Processing news RSS feeds and trying to display them with the time elapsed (like 2 min ago, 1 month ago). I'm using PrettyTime library for Android. It works fine for such dates formats

<pubDate>Thu, 04 Jan 2018 11:00:16 +0000</pubDate>.

However, if trying to parse, in following format

<updated>2017-12-30T11:10:44+00:00</updated>

app is crashing. That how I parse the dates in the RecyclerView

PrettyTime p = new PrettyTime(Locale.ENGLISH);
@Override
public void onBindViewHolder(final RecyclerViewAdapter.ViewHolder holder, int position) {
    setupImageLoader();

    final News newsItem = newsItems.get(position);
    String imgUrl = newsItem.getNewsImage();
    holder.date_updated.setText(p.format(new Date(newsItem.getUpdated())));
    holder.title.setText(newsItem.getTitle());

Any ideas how I can solve this issue?Thanks!!

Abhay Tomar
  • 175
  • 1
  • 9
PLab
  • 175
  • 1
  • 1
  • 9

1 Answers1

1

That's because Date class didn't understand given date format. You can use SimpleDateFormat class for parsing date. For your example code will be the next:

SimpleDateFormat format = new SimpleDateFormat("y-MM-dd'T'H:m:ssXXX");
Date date = new Date(format.parse(newsItem.getUpdated()));
Denysole
  • 3,903
  • 1
  • 20
  • 28