如何将RFC-1123日期时间格式转换为本地时间 [英] How to Convert RFC-1123 date-time formatter, to local time
问题描述
我正在此星期四,2016年8月25日08:59:00 GMT ="nofollow"> RFC 1123 格式在我的Android应用中.我需要转换为当地时间.
I am getting the date-time Thu, 25 Aug 2016 08:59:00 GMT
in this RFC 1123 format in my Android app. I need to convert to local time.
推荐答案
DateTimeFormatter.RFC_1123_DATE_TIME
java.time类可以直接通过 RFC 1123 解析/生成字符串. DateTimeFormatter.RFC_1123_DATE_TIME
常量.
DateTimeFormatter.RFC_1123_DATE_TIME
The java.time classes can directly parse/generate strings in the RFC 1123 via the DateTimeFormatter.RFC_1123_DATE_TIME
constant.
ZonedDateTime zdt =
ZonedDateTime.parse(
"Thu, 25 Aug 2016 08:59:00 GMT" ,
DateTimeFormatter.RFC_1123_DATE_TIME
)
;
从那里,调整为所需的时区.
From there, adjust into your desired time zone.
ZoneId z = ZoneId.of( "America/Montreal" ); // Or "Asia/Kolkata", etc.
ZonedDateTime zdtMontreal = zdt.withZoneSameInstant( z );
仅供参考, RFC 1123 格式很糟糕,如今已过时.格式的问题包括:假设为英语,难以解析,并且使用3-4个字母的区域缩写,这些缩写不是实时区域,不是标准化的,甚至也不是唯一的(!).相反:
FYI, that RFC 1123 format is terrible, and is outdated nowadays. The format’s problems include: assumes English, difficult to parse, and uses the 3-4 letter zone abbreviations that are not real time zones, not standardized, and are not even unique(!). Instead:
- 使用适当的IANA定义的时区指定时区.
- 使用标准 ISO 8601 格式序列化为文本.
java.time在解析/生成日期时间值的文本表示形式时,类默认使用这些格式.
- Specify time zones using proper IANA-defined time zones.
- Serialize to text using standard ISO 8601 formats.
The java.time classes use these formats by default when parsing/generating textual representations of date-time values.
搜索堆栈溢出以获取有关调整时区和生成日期时间值的字符串的更多信息.
Search Stack Overflow for more information about adjusting time zones and generating Strings of date-time values.
java.time 框架内置于Java 8及更高版本中.这些类取代了麻烦的旧版日期时间类,例如 Calendar
,& SimpleDateFormat
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在位于<一个href ="https://en.wikipedia.org/wiki/Maintenance_mode" rel ="nofollow noreferrer">维护模式,建议迁移到要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
您可以直接与数据库交换 java.time 对象.使用符合 java.sql.*
类.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 , Java SE 11 ,以及更高版本-具有捆绑实施的标准Java API的一部分.
- Java 9添加了一些次要功能和修复.
- 大多数 java.time 功能都向后移植到Java 6&7在> ThreeTen-Backport 中./li>
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- java.time 类的Android捆绑包实现的最新版本.
- 对于较早的Android(< 26), ThreeTenABP 项目适应> ThreeTen-Backport (如上所述).请参阅 如何使用ThreeTenABP… .
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
> ThreeTen-Extra 项目扩展了java.time与其他班级.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如
间隔
,年周"
,YearQuarter
和更多这篇关于如何将RFC-1123日期时间格式转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!