解析字符串到DateFormat的日期不正确解析 [英] Parsing a String into Date with DateFormat not parsing correctly

查看:142
本文介绍了解析字符串到DateFormat的日期不正确解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索,只是找不到这样的解释或原因,但是DateFormat的parse(String)方法只是不能正确解析我的String。

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.

我正在尝试将一个String解析为用于HTTP头的日期格式,并且获得了自己的String,如:

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

以下格式:

E, d MMM yyyy HH:mm:ss z

但是当我使用 df.parse(dateStr); 我得出结论:

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

Thu Nov 11 18:34:22 GMT 2010

哪个不是我想要的,为什么在GMT后的一年?为什么不再有逗号?为什么这个月份之后的日期?

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?

感谢您的时间,

Infinitifizz

Infinitifizz

PS

忘了提这个,但我已经尝试过dateformat.setLenient(false),没有任何区别。

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

PPS

我正在尝试将日期与date1.before(date2)和after()等进行比较,以查看是否有比其他更新,但我不能这样做,因为解析不起作用。

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.

即使它们看起来一样,但格式不同,它们是不一样,因为在他们两个调用getTime()之后(当我提供了2个相同的日期时)longs是不一样的。在日期是:

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

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

如果我输入字符串Thu,2010年11月11日19:38:52 GMT,然后比较他们的长度,一旦使用parse()将字符串转换为日期,然后在该日期调用getTime(),我得到:

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

lastModified = 1289504332671 fromString = 1289504332000

只有最后3位数字不一样,这有什么意义吗?

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

Infinitifizz

推荐答案

结果格式是 日期#toString() (点击链接查看javadoc)。你显然在做一个 System.out.println(date)。你想使用 SimpleDateFormat#format() ,而是使用另一种格式将其格式化为所需的格式。例如:

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);






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


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.

这篇关于解析字符串到DateFormat的日期不正确解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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