使用 DateFormat 无法正确解析字符串到日期

发布于 2024-10-01 15:30:17 字数 1107 浏览 4 评论 0原文

我一直在搜索,只是找不到发生这种情况的解释或原因,但 DateFormat 的 parse(String) 方法只是无法正确解析我的字符串。

我正在尝试将字符串解析为用于 HTTP 标头的日期格式,并尽可能获取字符串本身,例如:

Thu, 11 Nov 2010 18:34:22 GMT

其格式为:

E, d MMM yyyy HH:mm:ss z

但是当我使用 df.parse(dateStr ); 这就是我从中得到的结果:

Thu Nov 11 18:34:22 GMT 2010

这与我想要的完全不同,为什么现在是格林尼治标准时间之后的一年?怎么没有逗号了?为什么日期是在月份之后?

我现在对此完全感到困惑,无法找到解决方案,但我确实需要采用这种格式的日期。逗号把事情弄乱了吗?或者冒号?

感谢您抽出时间,

Infinitifizz

P.S.

忘了提及这一点,但我尝试过 dateformat.setLenient(false) ,但没有什么区别。

PPS

我试图这样做来将日期与 date1.before(date2) 和 after() 等进行比较,看看其中一个是否比另一个更新,但我不能这样做,因为解析不起作用。

尽管它们看起来相同,但只是格式不同,但它们并不相同,因为在它们两个上调用 getTime() 后(当我提供了 2 个相同的日期时),长整型并不相同。如日期所示:

Thu, 11 Nov 2010 19:38:52 GMT for a lastModified() on a File

如果我输入字符串“Thu, 11 Nov 2010 19:38:52 GMT”,然后在转换后比较它们的 long使用 parse() 将字符串转换为日期,然后在该日期调用 getTime() 我得到:

lastModified = 1289504332671 fromString = 1289504332000

只是最后3位不同,这有什么意义吗?

再次感谢您抽出宝贵的时间,抱歉我没有先把这一点放进去,

Infinitifizz

I've been searching all over and just can't find a explanation or reason why this is happening but the parse(String) method of DateFormat just isn't parsing my String correctly.

I'm trying to parse a String into the date format that is used for HTTP headers and got as far as getting the String on its own such as:

Thu, 11 Nov 2010 18:34:22 GMT

Which is in the format:

E, d MMM yyyy HH:mm:ss z

But when I use df.parse(dateStr); this is what I get out of it:

Thu Nov 11 18:34:22 GMT 2010

Which is nothing like what I wanted, why is the year now after the GMT? Why is there no comma anymore? And why is the date after the month?

I'm completely confused about this now and can't find a solution but I really need the date to be in that format. Is the comma messing things up? or the colons?

Thanks for your time,

Infinitifizz

P.S.

Forgot to mention this but I've tried dateformat.setLenient(false) and it makes no difference.

P.P.S

I'm trying to do this to compare the dates with date1.before(date2) and after() etc to see if one is newer than the other but I can't do this because the parsing isn't working.

Even though they look the same but just the format is different, they are not the same because after calling getTime() on both of them (When I have provided 2 identical dates) the longs are not the same. As in the date is:

Thu, 11 Nov 2010 19:38:52 GMT for a lastModified() on a File

If I input the String "Thu, 11 Nov 2010 19:38:52 GMT" and then compare their longs once converting the string to a date using parse() and then calling getTime() on that date I get:

lastModified = 1289504332671
fromString = 1289504332000

It is only the last 3 digits that are different, does this have any significance?

Thanks again for your time and sorry I didn't put this bit in first,

Infinitifizz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2024-10-08 15:30:17

结果格式为默认格式 Date#toString()(单击链接查看 javadoc)。您显然正在执行System.out.println(date)。您想使用 SimpleDateFormat#format() 而不是使用其他模式将其格式化为所需的格式。例如

String newDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

更新:使用日期时您不应该关心格式。您应该只关心将 Date 转换(显示)为 String 时的格式。至于时间戳的差异,Date 的时间戳使用毫秒精度,而 HTTP header 使用秒精度。您希望在比较之前将时间戳除以 1000。

The result format is the default format of Date#toString() (click link to see the javadoc). You're apparently doing a System.out.println(date). You would like to use SimpleDateFormat#format() instead with another pattern to format it in the desired format. E.g.

String newDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

Update: You shouldn't care about the format when using Date. You should only care about the format at that point when Date is to be converted (displayed) as String. As to the difference in timestamps, the Date uses millisecond precision for the timestamp while HTTP header uses second precision. You'd like to divide the timestamps by 1000 before comparing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文