date.compareTo() method doesn't work

211 views
Skip to first unread message

pipo

unread,
Aug 25, 2009, 10:52:07 AM8/25/09
to Google Web Toolkit
it seems the method Date.compareTo(Date myDate) does not work with the
compiled version on GWT client side code but work perfectly in Web
Hosted mode.
Is it normal or is it a bug?

Instead of compareTo() I compare two dates using getTime() method.

Thomas Broyer

unread,
Aug 25, 2009, 4:32:23 PM8/25/09
to Google Web Toolkit


On 25 août, 16:52, pipo <marchais.nico...@gmail.com> wrote:
> it seems the method Date.compareTo(Date myDate) does not work with the
> compiled version on GWT client side code but work perfectly in Web
> Hosted mode.

That's because hosted mode uses the pure-Java Date while web mode
("compiled version") uses a JavaScript emulation.

> Is it normal or is it a bug?

It isn't normal, but compareTo looks fine:
http://code.google.com/p/google-web-toolkit/source/browse/releases/1.7/user/super/com/google/gwt/emul/java/util/Date.java#132
...and further mode it is unit-tested:
http://code.google.com/p/google-web-toolkit/source/browse/releases/1.7/user/test/com/google/gwt/emultest/java/util/DateTest.java#123

marcotrev

unread,
Sep 12, 2009, 5:50:43 PM9/12/09
to Google Web Toolkit
Hi all,

I'm experiencing the same issue using GWT 1.7. Browsers tested:
Firefox 3.5.3 (Mac OSX), Safari 4 (Mac OSX), Firefox 3.0 (Linux).

I haven't tested the java.util.Date.compareTo(Date) output directly, I
simply found that the result of Collections.sort(List<MyBean>,
Comparator<MyBean>) is not correct if the comparator delegates the
return value to Date.compareTo(Date).

The result becomes correct if I replace the Date.compareTo(Date)
statement with a logically equivalent code snippet which uses
Date.getTime().

Regards,

Marco


On 25 Ago, 22:32, Thomas Broyer <t.bro...@gmail.com> wrote:
> On 25 août, 16:52, pipo <marchais.nico...@gmail.com> wrote:
>
> > it seems the method Date.compareTo(Date myDate) does not work with the
> > compiled version on GWT client side code but work perfectly in Web
> > Hosted mode.
>
> That's because hosted mode uses the pure-Java Date while web mode
> ("compiled version") uses a JavaScript emulation.
>
> > Is it normal or is it a bug?
>
> It isn't normal, but compareTo looks fine:http://code.google.com/p/google-web-toolkit/source/browse/releases/1....
> ...and further mode it is unit-tested:http://code.google.com/p/google-web-toolkit/source/browse/releases/1....

Sunshine Rain

unread,
Oct 29, 2009, 7:05:37 PM10/29/09
to Google Web Toolkit
Any word on this? I just verified that Date.compareTo() does not work
properly on Firefox,
Safari and Chrome on a Mac, but does work in hosted mode. I used a
TreeSet.

tskaife

unread,
Oct 30, 2009, 10:43:03 AM10/30/09
to Google Web Toolkit
I had the same problem, as well, I think it had something to do with
where I got the date object from for me (if I create 2 new date
objects on the front end it would work fine, but if I got one of them
from the database then I would have issues). I just ended up doing a
getTime() on both of my date objects to compare them so instead of...

date1.compareTo(date2)

I would do

date1.getTime() - date2.getTime()

I think I have them in the right order...
Reply all
Reply to author
Forward
0 new messages