1.Do 在 xml 布局中,我必须有两个视图,一个用于时间,一个用于日期? 但在 URL 中(当我阅读 JSON 格式化程序发布的信息时( (有时间和日期(
- 那么如何将 JSON 的时间戳转换为正常时间。
您可以说的时间或日期采用这种格式 2020-01-09T14:50:58.000Z 我应该在适配器文件中转换它,还是应该在 QueryUtils 中执行此操作,在那里我正在从 JSON 中创建和提取内容。
**My QueryUtils.java**
try {
JSONObject baseJsonResponse = new JSONObject(bookJson);
JSONArray newsArray = baseJsonResponse.getJSONArray("articles");
for (int i = 0; i < newsArray.length(); i++) {
JSONObject currentNews = newsArray.getJSONObject(i);
/*JSONObject properties = currentNews.getJSONObject("articles");*/
JSONObject newsSource = currentNews.getJSONObject("source");
String title = currentNews.getString("title");
String description = currentNews.getString("description");
String url = currentNews.getString("url");
/*String name = properties.getString("name");*/
String name = newsSource.getString("name");
String time = currentNews.getString("publishedAt");
String image = currentNews.getString("urlToImage");
News news = new News (title, description, url, name, time, image);
newss.add(news);
我的适配器 java 文件是
TextView dateView = (TextView) listItemView.findViewById(R.id.date);
dateView.setText(currentNews.getTime());
我想一起显示我的时间和日期,有人可以帮忙吗?
数据模型与表示
立即将输入字符串2020-01-09T14:50:58.000Z
解析为Instant
对象。末尾的Z
表示 UTC(零小时-分钟-秒的偏移量(。
Instant instant = Instant.parse( "2020-01-09T14:50:58.000Z" ) ;
将该Instant
对象存储在数据模型中。
在用户界面中演示时,请将Instant
(始终采用 UTC(调整为用户预期/期望的时区。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
然后让java.time自动本地化。指定一个Locale
以确定本地化中使用的人类语言和文化规范。Locale
与时区无关。
Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale ) ;
String output = zdt.format( f ) ;
这在Stack Overflow上已经多次介绍过了。因此,请搜索以了解更多信息。