在JAVA中解析RFC 2822日期
我需要解析 Java 中日期的 RFC 2822 字符串表示形式。示例字符串如下:
2010 年 3 月 13 日星期六 11:29:05 -0800
它看起来很糟糕,所以我想确保我做的一切都是正确的,并且稍后会遇到奇怪的问题,因为日期被解释为错误,无论是通过 AM-PM/军事时间问题, UTC 时间问题、我没有预料到的问题等等...
谢谢!
I need to parse an RFC 2822 string representation of a date in Java. An example string is here:
Sat, 13 Mar 2010 11:29:05 -0800
It looks pretty nasty so I wanted to make sure I was doing everything right and would run into weird problems later with the date being interpreted wrong either through AM-PM/Military time problems, UTC time problems, problems I don't anticipate, etc...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是执行您要求的快速代码(使用 简单日期格式)
PS。我在这里没有处理异常和并发(因为 SimpleDateFormat 在解析日期时不同步)。
This is quick code that does what you ask (using SimpleDateFormat)
PS. I've not dealt with exceptions and concurrency here (as SimpleDateFormat is not synchronized when parsing date).
如果您的应用程序使用英语以外的其他语言,您可能需要使用备用 SimpleDateFormat 构造函数强制日期解析/格式化的区域设置:
If your application is using another language than English, you may want to force the locale for the date parsing/formatting by using an alternate SimpleDateFormat constructor:
请记住,[星期几“,”] 在 RFC-2822 中是可选的,因此建议的示例并未涵盖所有 RFC-2822 日期格式。此外,RFC-822 日期类型允许许多不同的时区符号(obs-zone),“Z”格式说明符未涵盖这些符号。
我想除了寻找“,”和“-|+”来确定使用哪种模式之外,没有简单的方法。
Please keep in mind that the [day-of-week ","] is optional in RFC-2822, hence the suggested examples are not covering all RFC-2822 date formats. Additional, the RFC-822 date type allowed many different time zone notations(obs-zone), which are not covered by the "Z" format specifier.
I guess there is no easy way out, other than looking for "," and "-|+" to determine which pattern to use.
DateTimeFormatter.RFC_1123_DATE_TIME
自 Java 8 以来,实现了新的日期时间类:
java.time.ZonedDateTime
和java.time.LocalDateTime
。ZonedDateTime
支持几乎开箱即用的 RFC 字符串解析:DateTimeFormatter.RFC_1123_DATE_TIME
Since Java 8 new datetime classes were implemented:
java.time.ZonedDateTime
andjava.time.LocalDateTime
.ZonedDateTime
supports the parsing of RFC strings nearly out of the box:有一个 javax.mail 类执行 RFC-2822 日期的解析:
javax.mail.internet.MailDateFormat
包括可选和过时的格式。
只需这样做:
它将正确解析这些有效的 RFC-2822 日期
对于其他旧的 DateFormatters,
MailDateFormat
类不是线程安全的。There is a javax.mail class that perform the parsing of RFC-2822 dates :
javax.mail.internet.MailDateFormat
including the optional and obsolete formats.
Just do :
It will correctly parse these valid RFC-2822 dates
As for other old DateFormatters, the
MailDateFormat
class is not thread safe.RFC 2822 日期时间字符串包含时区偏移量,例如给定字符串 Sat, 13 Mar 2010 11:29:05 -0800 与 UTC 的时区偏移量为 -08:00 小时,即等效日期时间UTC 时间可以通过在2010 年 3 月 13 日星期六 11:29:05 基础上加上 8 个小时来获得。
java.time
现代日期时间 API API 有
OffsetDateTime
来表示带有时区偏移的日期时间。输出:
从 跟踪:日期时间。
一些有用的链接:
y
代替u
,但我更喜欢u
代码> 到y
。OffsetDateTime
与 JDBC 结合使用。RFC 2822 date-time string contains timezone offset e.g. the given string Sat, 13 Mar 2010 11:29:05 -0800 has a timezone offset of -08:00 hours from UTC i.e. the equivalent date-time at UTC can be obtained by adding 8 hours to Sat, 13 Mar 2010 11:29:05.
java.time
The modern date-time API API has
OffsetDateTime
to represent a date-time with timezone offset.Output:
Learn more about the modern Date-Time API from Trail: Date Time.
Some useful links:
y
instead ofu
but I preferu
toy
.OffsetDateTime
with JDBC.试试这个:
Try this: