Java 中的 RFC822 时区解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2823442/
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
RFC822 Timezone Parsing in Java
提问by Jamen
I have a JS date that is being converted by Dojo into RFC822 format. The function call - dojo.date.toRfc3339(jsDate), generates the following date - 2007-02-26T20:15:00+02:00.
我有一个由 Dojo 转换为 RFC822 格式的 JS 日期。函数调用 - dojo.date.toRfc3339(jsDate),生成以下日期 - 2007-02-26T20:15:00+02:00。
I have an application that uses a Java date SimpleDateFormat to parse in the dates generated above. I am having problems parsing this date format due to the timezone. I have attempted to use
我有一个应用程序,它使用 Java 日期 SimpleDateFormat 来解析上面生成的日期。由于时区,我在解析此日期格式时遇到问题。我曾尝试使用
yyyy-mm-DD'T'hh:mm:ssZ
yyyy-mm-DD'T'hh:mm:ssZ
This fails as the 'Z' for timezone doesn't expect a ':' character. Does anyone know how I would specify a pattern to handle a RFC822 date with the ':'?
这失败了,因为时区的 'Z' 不需要 ':' 字符。有谁知道我将如何指定一个模式来处理带有 ':' 的 RFC822 日期?
revision:
修订:
Thanks for correctly interpreting what I am trying to do :) I was meant to say the date is generating in RFC3339 and I needed RFC822. Looks like I will have to override the JavaScript. I was hoping that I wouldn't have to do that and could specify a date format pattern without having to modify any Java Code as the date format is simply injected into a Spring bean of an application.
感谢您正确解释我正在尝试做的事情:) 我的意思是说日期是在 RFC3339 中生成的,我需要 RFC822。看起来我将不得不覆盖 JavaScript。我希望我不必这样做,并且可以指定日期格式模式而无需修改任何 Java 代码,因为日期格式只是注入到应用程序的 Spring bean 中。
Just for completeness, is there a way to specify in a date format expression to ignore characters in the sequence (without doing String manipulation/replacement)? In this case I'd be saying ignore any ':' or just ignore the timezone all together?
只是为了完整性,有没有办法在日期格式表达式中指定忽略序列中的字符(不进行字符串操作/替换)?在这种情况下,我会说忽略任何“:”还是一起忽略时区?
回答by Alen
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static Date ParseRFC3339DateFormat(String p_date)
{
try{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String dts = p_date.replaceAll("([\+\-]\d\d):(\d\d)","");
return formatter.parse(dts);
}catch (Exception e) {
return null;
}
}
}
回答by mdma
This works
这有效
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-DD'T'hh:mm:ssZ");
format.parse("2007-02-26T20:15:00+02:00".replaceAll("([\+\-]\d\d):(\d\d)",""));
(Note I've taking the final colon out of the format after the 'Z' format specifier.)
(请注意,我在“Z”格式说明符之后的格式中去掉了最后一个冒号。)
回答by laz
RFC822 does not allow a colon to be in the time zone portion of date. It expects just the 4 digits. The name of that Dojo method indicates that it is using RFC3339. It seems that is practically the same format as ISO8601. It just so happens that Joda Timehas ISODateTimeFormatwhich is ISO8601 compatible if you are able to use that library. The method dateTimeNoMillis()looks like a match with the Dojo format. It really is nicer than the standard Java date and time API. Otherwise highlycaffeinated's suggestion would be the way to go.
RFC822 不允许冒号出现在日期的时区部分。它只需要 4 位数字。该 Dojo 方法的名称表明它使用的是 RFC3339。似乎与ISO8601 的格式几乎相同。它只是恰巧,约达时间已经ISODateTimeFormat是ISO8601兼容的,如果你能使用该库。该方法dateTimeNoMillis()看起来与 Dojo 格式相匹配。它确实比标准的 Java 日期和时间 API 更好。否则高咖啡因的建议将是要走的路。
Updated in response to Jamen's update
更新以响应 Jamen 的更新
Isn't there a way to have Dojo use a format that doesn't include the timezone? Then you can adjust the format on the Java side to match. I don't know much about Dojo and I haven't been able to find any documentation on the toRfc3339 function it provides.
没有办法让 Dojo 使用不包含时区的格式吗?然后你可以在Java端调整格式来匹配。我对 Dojo 了解不多,也找不到关于它提供的 toRfc3339 函数的任何文档。
回答by Daniel De León
In Java 8 you can use:
在 Java 8 中,您可以使用:
Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse( rfc822Time ) );
FYI: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME
仅供参考:https: //docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME
回答by Gordon
Instead of using dojo.date.toRfc3339(jsDate) you could create your own function with a custom format string.
您可以使用自定义格式字符串创建自己的函数,而不是使用 dojo.date.toRfc3339(jsDate)。
Something like the following would remove the colon and should be parsable by your java format.
类似以下内容将删除冒号,并且应该可以通过您的 java 格式进行解析。
function toRfc3339String(jsDate){
return dojo.date.locale.format(jsDate,{selector: 'date', datePattern:'yyyy-MM-dd\'T\'hh:mm:ssZ'});
}
回答by PNS
You can do this generically without Java 7. I have asked 2 questions in StackOverflowon this, within 2012, so there is a solution that does not need any third party libraries.
您可以在没有 Java 7 的情况下进行一般操作。我StackOverflow在 2012年内就此提出了 2 个问题,因此有一个不需要任何第三方库的解决方案。
Just check the implementation presented in the description of my latest question, which also points to the earlier one that discusses exactly this issue.
只需检查我的最新问题的描述中提供的实现,它也指向前面讨论过这个问题的实现。
回答by highlycaffeinated
I'd strip the ':' out of the timezone and use the format you have above.
我会从时区中删除 ':' 并使用上面的格式。

