-2 java datetime android json epoch
我对日期有点困惑。我目前正在开发天气应用程序,一切正常..我只是想将这种类型的格式处理成我自己想要的格式。
2017-09-10T18:35:00+05:00
Run Code Online (Sandbox Code Playgroud)
我只想将此日期转换为大纪元时间,然后以我想要的格式确定日期::
对于J-SON
或者我想将此日期转换为更少的数字,即 Sun,9 月 9 日 9:23 Am 等。
其他答案是正确的,但在编写之前就已经过时了。最近,我建议您使用现代 Java 日期和时间 API,称为 JSR-310 或java.time. 您的日期时间字符串格式是 ISO 8601,现代类 \xe2\x80\x9c 将 \xe2\x80\x9d 理解为默认格式。
您可以在 Android 上使用现代 API 了吗?当然!JSR-310 类已在ThreeTenABP项目中向后移植到 Android。所有细节都在这个问题中:How to use ThreeTenABP in Android Project。
\n long epochTime = OffsetDateTime.parse("2017-09-10T18:35:00+05:00")\n .toInstant()\n .getEpochSecond();\nRun Code Online (Sandbox Code Playgroud)\n结果是 1505050500。
\n编辑:Arvind Kumar Avinash 在评论中正确指出:您不需要将 an 转换OffsetDateTime为 anInstant来获取纪元秒。您可以简单地使用OffsetDateTime#toEpochSecond.
如何将其转换为人类可读的日期和时间的示例:
\n String formattedDateTime = Instant.ofEpochSecond(epochTime)\n .atZone(ZoneId.of("Africa/Lusaka"))\n .format(DateTimeFormatter.ofPattern("EEE, d MMMM h:mm a", Locale.ENGLISH));\nRun Code Online (Sandbox Code Playgroud)\n这会产生Sun, 10 September 3:35 PM. 请提供您想要的时区 ID 的正确地区和城市。如果您想依赖 device\xe2\x80\x99s 时区设置,请使用ZoneId.systemDefault(). 请参阅DateTimeFormatter.ofPattern() 的文档,了解您可以在格式模式字符串中使用的字母,或者用于DateTimeFormatter.ofLocalizedDateTime()您的 locale\xe2\x80\x99s 默认格式之一。