为以前的 OS 版本解析 Java 中的 DateTime

Parsing DateTime in Java for previous OS versions

我一直在练习使用 The Guardian 的 API 为 android 应用解析 JSON,但我在将日期格式化为更好看的格式时遇到了问题。 这是我的适配器到目前为止的样子: public class ArticleAdapter 扩展 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;
}
}

我尝试过使用字符串或日期对象作为 formatDate() 的输入,以及许多不同的格式化技术 -

使用输入和输出日期格式 使用 .formatter 而不是 .parse 我从类似问题中找到的其他几个解决方案,老实说不能全部记住。

应用程序不断崩溃并显示“IllegalArgumentException”。通过足够多的试验和错误,我能够追踪故障以确定它来自这里。

注:getDate()方法returns一个String JSON 被解析的小例子:

[{"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"}

我使用的日期是 webPublicationDate

这是我第一次发布问题,希望我没有遗漏描述中的任何内容。感谢任何指导,因为我有点迷路了。

好的,经过更多的工作,我能够找出导致失败的原因:

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

当我评论这一行时,一切都按预期进行。我修复了它,但我仍然不确定为什么,有人可以向我解释一下吗?

提前致谢!

原因是日期 "webPublicationDate":"2018-07-04T23:01:14Z" 已经格式化为 UTC 时区,所以您需要先使用 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);