Search code examples
javaandroidjsonillegalargumentexception

Parsing DateTime in Java for previous OS versions


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.


Solution

  • 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!