【发布时间】:2017-09-12 05:59:07
【问题描述】:
我收到的 json 响应为 "publishedAt": "2017-09-12T01:03:08Z"
我想用简单的日期格式化这个响应,比如Aug 12-09-2017 3:08并将其设置为TextView
我正在使用这样的东西
Date dateObj = new Date(currentNews.getTimeInString());
TextView dateAndTimeView = (TextView) listItemView.findViewById(R.id.date_and_time);
String formattedDateAndTime = formatDateAndTime(dateObj);
dateView.setText(formattedDate);
private String formatDateAndTime(Date dateObj){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM, yyyy");
return dateFormat.format(dateObj);
}
【问题讨论】:
-
到目前为止你尝试了什么??
-
请在代码中分享您的尝试。
-
即使在 Android 上你很想使用早已过时的类
Date和SimpleDateFormat(因为没有更好的内置),我当然会警告不要使用已弃用的Date(String)构造函数。更好的是,停止使用这些类并开始使用the modern Java date and time API。要在 Android 上使用它,您需要 ThreeTenABP,请参阅 this question。 -
感谢您的代码。顺便说一句,欢迎来到 Stack Overflow。您还应该说明您的代码行为与您想要的行为有何不同。如果您看到任何错误消息和/或堆栈跟踪,请逐字引用。
-
“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括……一个特定的问题或错误……。没有明确问题陈述的问题对其他读者没有用处。” 引自What topics can I ask about here?