egervari
egervari

Reputation: 22512

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

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

Upvotes: 3

Views: 3131

Answers (2)

Mozammel
Mozammel

Reputation: 1051

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"

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 3

Related Questions