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

问题描述 投票:0回答:2

我一直在练习使用The Guardian的API解析Android应用程序的JSON,而我在将日期格式化为更好看的格式时遇到了麻烦。这是我的适配器到目前为止的样子:公共类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;
}
}

我已经尝试使用String as或Date对象作为formatDate()的输入,以及许多不同的格式化技术 -

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

该应用程序不断崩溃与“IllegalArgumentException”。通过足够的试验和错误,我能够追踪失败,以确定它是从这里来的。

注意:getDate()方法返回一个被解析的JSON的String Small示例:

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

这是我第一次发布问题所以我希望我没有错过任何描述。欣赏任何方向,因为我有点失落。

java android json illegalargumentexception
2个回答
0
投票

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

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

一旦我评论了这一行,一切都按预期工作。我修好了但是我仍然不确定为什么,有人可以向我解释这个吗?

提前致谢!


0
投票

原因是日期“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);
© www.soinside.com 2019 - 2024. All rights reserved.