在JAVA中解析RFC 2822日期 [英] Parsing RFC 2822 date in JAVA

查看:875
本文介绍了在JAVA中解析RFC 2822日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


2010年3月13日星期六11:29:05 -0800

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

看起来很讨厌,所以我想确保我正在做的一切正确,并会遇到奇怪的问题后,日期被解释错误通过AM-PM /军事时间问题,UTC时间问题,我不期待的问题等...

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...

谢谢!

推荐答案

这是一个快速的代码,可以做你所要求的(使用 SimpleDateFormat

This is quick code that does what you ask (using 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。我没有在这里处理异常和并发(因为SimpleDateFormat在解析日期时不同步)。

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

这篇关于在JAVA中解析RFC 2822日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆