在 JAVA 中解析 RFC 2822 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2458524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:15:31  来源:igfitidea点击:

Parsing RFC 2822 date in JAVA

javadateparsingrfc2822

提问by Chris Dutrow

I need to parse an RFC 2822 string representation of a date in Java. An example string is here:

我需要解析 Java 中日期的 RFC 2822 字符串表示。一个示例字符串在这里:

Sat, 13 Mar 2010 11:29:05 -0800

2010 年 3 月 13 日星期六 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...

它看起来很糟糕,所以我想确保我做的一切都是正确的,并且以后会遇到奇怪的问题,因为 AM-PM/军事时间问题、UTC 时间问题、我没想到的问题等,日期被解释错误...

Thanks!

谢谢!

回答by Buhake Sindi

This is quick code that does what you ask (using SimpleDateFormat)

这是执行您要求的快速代码(使用SimpleDateFormat

String rfcDate = "Sat, 13 Mar 2010 11:29:05 -0800";
String pattern = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date javaDate = format.parse(rfcDate);

//Done.

PS. I've not dealt with exceptions and concurrency here (as SimpleDateFormat is not synchronized when parsing date).

附注。我没有在这里处理异常和并发(因为 SimpleDateFormat 在解析日期时不同步)。

回答by Laurent VB

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:

如果您的应用程序使用的不是英语的其他语言,您可能希望通过使用备用 SimpleDateFormat 构造函数来强制日期解析/格式化的语言环境:

String pattern = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);

回答by Casper

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.

请记住,[星期几“,”] 在 RFC-2822 中是可选的,因此建议的示例并未涵盖所有 RFC-2822 日期格式。此外,RFC-822 日期类型允许许多不同的时区符号(obs-zone),“Z”格式说明符未涵盖这些符号。

I guess there is no easy way out, other than looking for "," and "-|+" to determine which pattern to use.

我想没有简单的出路,除了寻找“,”和“-|+”来确定使用哪种模式。

回答by pathfinder78

Since Java 8 new datetime classes were implemented: java.time.ZonedDateTimeand java.time.LocalDateTime. ZonedDateTimesupports the parsing of RFC strings nearly out of the box:

由于实现了 Java 8 新的日期时间类:java.time.ZonedDateTimejava.time.LocalDateTime. ZonedDateTime支持几乎开箱即用的 RFC 字符串解析:

String rfcDate = "Tue, 4 Dec 2018 17:37:31 +0100 (CET)";  
if (rfcDate.matches(".*[ ]\(\w\w\w\)$")) {
    //Brackets with time zone are added sometimes, for example by JavaMail
    //This must be removed before parsing
    //from: "Tue, 4 Dec 2018 17:37:31 +0100 (CET)"
    //  to: "Tue, 4 Dec 2018 17:37:31 +0100"
    rfcDate = rfcDate.substring(0, rfcDate.length() - 6);
}

//and now parsing... 
DateTimeFormatter dateFormat = DateTimeFormatter.RFC_1123_DATE_TIME;
try {
    ZonedDateTime zoned = ZonedDateTime.parse(rfcDate, dateFormat);
    LocalDateTime local = zoned.toLocalDateTime();        
} catch (DateTimeParseException e) { ... }