android - 结果不正确 : Formatting ISO-8601 Time And Extract 12 Hour Time

标签 android facebook datetime facebook-fql iso8601

我正在使用 FQL 从 Facebook API 获取新闻源,它返回一个“created_time”字段作为 UNIX 时间戳。我正在使用这段代码将其转换成我认为是 ISO-8601 时间戳:

String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
String finalCreatedTime = dateFormatter.format(new Date(finalTimeStamp * 1000L));

现在,我想从字符串 finalCreatedTime 中提取 12 小时制 (AM/PM) 格式的时间。

为此,我使用了这段代码:

final String old_format = "yyyy-MM-dd'T'HH:mm:ssZ";
final String new_format = "EEE MMM d hh:mm aa yyyy";

String oldDateSring = finalCreatedTime;

SimpleDateFormat sdf = new SimpleDateFormat(old_format);
Date d = sdf.parse(oldDateSring);
sdf.applyPattern(new_format);

Calendar cal = Calendar.getInstance();
cal.setTime(d);

和:

// GET THE TIME
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(cal.getTime());
feeds.setTime(getTime);
Log.e("TIME", getTime);

第一个代码块的结果是:2012-12-14T04:30:03+0530

//GET THE TIME block 的结果是 04:30AM 而它应该是 04:30PM

我将不胜感激任何关于此的指示。也许,我实现错了?

编辑:我可能会补充说,中午 12 点到下午 1 点之间的时间戳得到了正确处理,并按应有的方式显示下午。

最佳答案

你有:

String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
// Note 8601 is written with 'HH'
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

到目前为止,还不错。但是你需要从中创建一个日期。

Date date = new Date(finalTimeStamp * 1000L)

然后,您需要格式化您需要的内容(永远不要解析您刚刚格式化的日期。这根本没有意义)。

String finalCreatedTime = dateFormatter.format(date); // Not sure if you need this one

SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(date);
feeds.setTime(getTime);

关于android - 结果不正确 : Formatting ISO-8601 Time And Extract 12 Hour Time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13878195/

相关文章:

java.lang.NumberFormatException : Invalid int: "1"

android - 基于位置的 Android 应用架构的最佳实践是什么?

android - 出现2个代码时,Google Vision崩溃

android - WebView.zoomIn() 在呈现 View 之前不起作用

java - 新的(但不同的)Facebook ApplicationId 导致 NPE

ios - Facebook SDK 问题

php - 如何通过 curl 获取 facebook token 代码?

php - 如何从 symfony2/php 中的字符串创建 DateTime 对象

javascript - 我可以让我的浏览器像在另一个语言环境中一样呈现页面吗?

python-3.x - CSV 计数重复项