@DateTimeFormat in Spring produces off-by-one day error

守給你的承諾、 提交于 2019-12-12 16:14:02

问题


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

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

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

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.

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

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

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

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.

I hate Java :(

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?

Thanks


回答1:


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.

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.




回答2:


I've found that starting your JVM with your local timezone specified in the arguments solves this issue. For me it was just adding this line to the run configuration:

-Duser.timezone="Asia/Dhaka"


来源:https://stackoverflow.com/questions/7817898/datetimeformat-in-spring-produces-off-by-one-day-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!