-1

I've been practicing on parsing JSON for android apps using The Guardian's API, and I'm having trouble formatting the date into a better looking format. This is what my adapter looks like so far: public class ArticleAdapter extends ArrayAdapter {

public ArticleAdapter(MainActivity context, ArrayList<Article> article){
    super(context,0, article);
}

/**
 * Return the formatted date string
 */
private String formatDate(String dateObject) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        return formatter.parse(dateObject).toString();

}


@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.article_list_item, parent, false);
    }
    Article currentArticle = getItem(position);

    TextView header = (TextView) listItemView.findViewById(R.id.header);
    header.setText(currentArticle.getHeader());

    //Creates the date view and object and passing it through the function to format properly
    TextView date = (TextView) listItemView.findViewById(R.id.date);
    Date DateObject = new Date(currentArticle.getDate());
    try {
        date.setText(formatDate(currentArticle.getDate()));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return listItemView;
}
}

I've tried both using a String as or a Date object as the input to formatDate(), as well as many different formatting techniques -

using an input and output date format using .formatter rather than .parse several other solutions I found from similar questions, can't honestly remember them all.

The app keeps crashing with "IllegalArgumentException". Through enough trial and error I was able to trace the failure to be certain it's coming from here.

Note: The getDate() method returns a String Small example of the JSON being parsed:

[{"id":"technology/2018/jul/05/privacy-policies-facebook-amazon-google-not-gdpr-compliant","type":"article","sectionId":"technology","sectionName":"Technology","webPublicationDate":"2018-07-04T23:01:14Z","webTitle":"Privacy policies of tech giants 'still not GDPR-compliant'","webUrl":"https://www.theguardian.com/technology/2018/jul/05/privacy-policies-facebook-amazon-google-not-gdpr-compliant","apiUrl":"https://content.guardianapis.com/technology/2018/jul/05/privacy-policies-facebook-amazon-google-not-gdpr-compliant","isHosted":false,"pillarId":"pillar/news","pillarName":"News"}

The date I'm using is webPublicationDate

This is the first time I've posted a question so I hope I didn't miss anything in the description. Appreciate any direction because I'm kind of lost atm.

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51

2 Answers2

0

Ok so after some more work I was able to find out what caused the failures:

Date DateObject = new Date(currentArticle.getDate()); //currentArticle.getDate() is a string

As soon as I commented this line out everything worked as expected. I fixed it but I'm still not sure as to why, could anyone explain this to me maybe?

Thanks in advance!

0

the reason is that the date "webPublicationDate":"2018-07-04T23:01:14Z" is already formatted in UTC timezone so you need to deconstruct it first using SimpleDateFormat

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss'Z'");

Date myDate = null;
        try {
            myDate = dateFormat.parse(currentArticle.getmDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }

// Define a new SimpleDateFormat object to reconstruct the date into the desired format.
        DateFormat newDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
        // Convert the Date object into a String.
        String formattedDate = newDateFormat.format(myDate);
TarekB
  • 757
  • 14
  • 20
  • Happy to help @Goreflox, please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – TarekB Apr 26 '19 at 11:31