Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT java.util.Date serialization bug

GWT doesn't serialize Java Date properly. When I tried sending Date created in Javascript through the wire, I found out that dates between April 1st (funny) and 25th October for years before year 1983 get subtracted by one day.

That means that, say, both 1982-04-01 and 1982-03-31 become 1982-03-31 on the Java side.

Given the dates in question, I would guess that this is some kind of DST problem. I've tried googling, and found only one other reference that describes similar problem.

I also tried submitting bug to the GWT team, but curiously wasn't able to find bugtracker for GWT.

So, my questions are:

  1. Anyone else run into this? I'm on GWT 1.7, and would like to confirm if this happens on 2.0 as well.

  2. My workaround was to send dates as Strings, and parse them on server. Anyone knows better workaround?

like image 846
Domchi Avatar asked Jan 20 '10 14:01

Domchi


2 Answers

Assuming that you are using a java.util.Date

Question 1: It seems that it is fixed in 2.0. I've created both Dates above (1982-04-01 and 1982-03-31) and they come through correctly to the server (both represent on the server as 1982-04-01 and 1982-03-31 respectively). My setup is:

  • GWT 2.0
  • Java 1.6
  • OSX 10.6.2

Question 2: You could always pass the 'milliseconds since January 1, 1970, 00:00:00 GMT' over the async service-which you can get using getTime() on the date object. On the server side you can then instantiate a new Date passing this value in on the constructor:
Date date = new Date(millis);
This saves fiddling around with formatters and parsers.

like image 135
Raymond Barlow Avatar answered Oct 21 '22 16:10

Raymond Barlow


Dates and times are a complicated subject. The conversion can depend on the locale that the browser is running in and wether both you JVM on the server and the locales of the clients are up-to-date.

In some cases there can be differences because in some regions they switched timezones in the past.

GWT sends dates using just millis since epoch time. Since it is using Date objects, you are limited in that the Dates on the server side will be automatically modified to the servers timezone. Are you sure that you take into account the possible difference in timezones between client and server ?

David

like image 25
David Nouls Avatar answered Oct 21 '22 16:10

David Nouls