如何在 Java 8 中将 'Thu, 22 Dec 2016 09:50:06 PST' 解析为 sql 时间戳
How to parse 'Thu, 22 Dec 2016 09:50:06 PST' into an sql TimeStamp in Java 8
RSS 规范指出日期时间字符串应符合 RFC 822。示例:
Sat, 07 Sep 2002 00:00:01 GMT.
我可以使用:
TimeStamp timestamp = TimeStamp.parse("Sat, 07 Sep 2002 00:00:01 GMT")
将 RFC 822 格式的字符串存储到 Derby 中。但是,我有一个 Feed 的日期格式如下:
Thu, 22 Dec 2016 09:50:06 PST
不符合 RFC 822/ISO8061 标准,对吗?这是什么格式?我怎样才能使它符合 RFC 822 标准,并且如果提要可能向我抛出的每个不符合标准的日期时间字符串都没有 DateTimeFormatter,我怎么能预测这些?
格式“2016 年 12 月 22 日星期四 09:50:06 PST”不符合 RFC-822 标准,原因超出您的想象。 RFC-822 (paragraph 5) and stands for GMT-07:00. However, the format given above is not compliant because it uses four-digit-year and not two-digit-year. This strong limitation was then corrected in RFC-1123 明确支持区域名称 PST,它说:
All mail software SHOULD use 4-digit years in dates, to ease the
transition to the next century.
另一个问题是如何在Java中支持它。在 Java-8-support 不够用的所有情况下,最好的方法是使用自定义模式编写您自己的格式化程序。 Java-8 仅支持使用 GMT 前缀的第一种格式,但不支持 DateTimeFormatter.RFC_1123_DATE_TIME
中的 PST:
RFC-1123 updates RFC-822 changing the year from two digits to four.
This implementation requires a four digit year. This implementation
also does not handle North American or military zone names, only 'GMT'
and offset amounts.
更新:
与此同时,我发布了我的库 Time4J-v.22 的新版本,它也支持北美时区缩写。它被实现为常量 ChronoFormatter.RFC_1123。使用示例:
Instant instant =
ChronoFormatter.RFC_1123.parse("Thu, 22 Dec 2016 09:50:06 PST").toTemporalAccessor();
目前仅支持军事区,我不打算这样做,因为 RFC-822 规范对所有军事区(“Z”除外)都有符号错误。因此,您可以使用这种简单而高效的方式(添加额外的依赖项)或编写一个由许多基于 java.time.format.DateTimeFormatter
.
实例的不同格式组成的解析器
RSS 规范指出日期时间字符串应符合 RFC 822。示例:
Sat, 07 Sep 2002 00:00:01 GMT.
我可以使用:
TimeStamp timestamp = TimeStamp.parse("Sat, 07 Sep 2002 00:00:01 GMT")
将 RFC 822 格式的字符串存储到 Derby 中。但是,我有一个 Feed 的日期格式如下:
Thu, 22 Dec 2016 09:50:06 PST
不符合 RFC 822/ISO8061 标准,对吗?这是什么格式?我怎样才能使它符合 RFC 822 标准,并且如果提要可能向我抛出的每个不符合标准的日期时间字符串都没有 DateTimeFormatter,我怎么能预测这些?
格式“2016 年 12 月 22 日星期四 09:50:06 PST”不符合 RFC-822 标准,原因超出您的想象。 RFC-822 (paragraph 5) and stands for GMT-07:00. However, the format given above is not compliant because it uses four-digit-year and not two-digit-year. This strong limitation was then corrected in RFC-1123 明确支持区域名称 PST,它说:
All mail software SHOULD use 4-digit years in dates, to ease the transition to the next century.
另一个问题是如何在Java中支持它。在 Java-8-support 不够用的所有情况下,最好的方法是使用自定义模式编写您自己的格式化程序。 Java-8 仅支持使用 GMT 前缀的第一种格式,但不支持 DateTimeFormatter.RFC_1123_DATE_TIME
中的 PST:
RFC-1123 updates RFC-822 changing the year from two digits to four. This implementation requires a four digit year. This implementation also does not handle North American or military zone names, only 'GMT' and offset amounts.
更新:
与此同时,我发布了我的库 Time4J-v.22 的新版本,它也支持北美时区缩写。它被实现为常量 ChronoFormatter.RFC_1123。使用示例:
Instant instant =
ChronoFormatter.RFC_1123.parse("Thu, 22 Dec 2016 09:50:06 PST").toTemporalAccessor();
目前仅支持军事区,我不打算这样做,因为 RFC-822 规范对所有军事区(“Z”除外)都有符号错误。因此,您可以使用这种简单而高效的方式(添加额外的依赖项)或编写一个由许多基于 java.time.format.DateTimeFormatter
.