@DateTimeFormat在Spring中会产生一个一天的错误 [英] @DateTimeFormat in Spring produces off-by-one day error

查看:676
本文介绍了@DateTimeFormat在Spring中会产生一个一天的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在域对象中使用@DateTimeFormat,如下所示:

I am currently using @DateTimeFormat in a domain object as follows:

@DateTimeFormat(pattern = "MM/dd/yyyy")
private Date startDate = new Date();

在Spring MVC控制器中,我发布今天的日期:10/19/2011使用jQuery UI日期选择器,我确认这是使用firebug作为HTTP Post参数发送,如下所示:

In a Spring MVC Controller, I am posting today's date: 10/19/2011 using the jQuery UI Date picker, and I confirm that this is being sent as an HTTP Post parameter using firebug as follows:

startDate=10%2F19%2F2011

不幸的是,一旦到达服务器上的Spring,它将日期存储为10/18 / 2011 - 有一个一日一天的错误。

Unfortunately, once it gets to Spring on the server, it stores the date as 10/18/2011 - there is an off-by-one day error.

我的代码中没有什么可以远程触摸日期 - 没有计算或任何事情关于这个日期。

There is nothing in my code that even remotely touches the date - there is no calculations or anything going on with regards to this date.

有什么关于@DateTimeFormat我应该注意吗?

Is there something about @DateTimeFormat that I should be aware of?

可以Hibernate中的某些内容也负责更改日期?

Could something in Hibernate be responsible for changing the date too?

我也在查看我的数据库以获取此应用程序。我正在存储另一个日期,称为 creationDate ,这是一个实际的时间戳,与用户输入不同。在大多数情况下,日期是相同的,但是客户希望能够设置不同的方式,这就是 startDate 是为。

I'm also looking at the my database for this application. I am storing another date, called creationDate which is an actual timestamp and differs from user input. In most cases, the dates are the same but the client wanted the ability to set it differently so that is what startDate is for.

Start Date              Creation Date (actual timestamp, not user input)
2011-04-17 19:00:00     2011-04-17 21:32:27
2011-04-18 19:00:00     2011-04-18 21:14:01
2011-04-20 19:00:00     2011-04-20 23:06:47
2011-04-26 19:00:00     2011-04-26 23:24:34
2011-04-28 19:00:00     2011-04-28 20:07:06
2011-05-01 19:00:00     2011-05-02 13:35:37
2011-06-21 19:00:00     2011-06-22 15:06:36
2011-07-28 19:00:00     2011-07-29 15:32:35
2011-09-03 19:00:00     2011-09-04 13:11:45
2011-10-11 19:00:00     2011-10-12 11:45:14
2011-10-11 19:00:00     2011-10-12 11:49:55
2011-10-18 19:00:00     2011-10-19 02:20:43

起初似乎是一个bug从5月开始,但后来我意识到日期是正确的,如果超过19:00:00,如果不在19:00:00,则一个接一个。

At first it seems like it was a bug started in May, but then I realized that the date is correct if it was over 19:00:00, and it's off-by-one if it's under 19:00:00.

我讨厌Java:(

发生时,Spring创建一个给定的日期 10/19/2011 - 似乎将该用户输入并将其格式化为 2011-10-18 19 :00:00

The problem seems to occur when Spring creates a date given 10/19/2011 - it seems to translate that user input and formats it to 2011-10-18 19:00:00.

最简单的解决方案是什么?

What is the simplest solution?

/ p>

Thanks

推荐答案

对我来说,这似乎很可能是时区问题。一个日期对象代表了一个即时的时间 - 我怀疑你是否看到你所拥有的确切的值(例如在UTC中,以保持事情清楚),你会更好地了解发生了什么。有可能你会看到10/18/2011,你会在不同的时区解释它。

It seems very likely to me that this is actually a matter of time zones. A Date object represents an instant in time - I suspect if you look at the exact value that you've got (e.g. in UTC, to keep things clear) you'll get a better idea of what's going on. Chances are that where you're seeing "10/18/2011" you're interpreting it in a different time zone.

如果Spring支持转换为 Joda时间类型我建议改为使用 - 然后您可以使用 LocalDate 真正 意味着一个日期,而不是一个即时的时间。

If Spring supports converting to Joda Time types I'd suggest using that instead - then you can use LocalDate which really does mean a date instead of an instant in time.

这篇关于@DateTimeFormat在Spring中会产生一个一天的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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