2

I have seen countless forum posts complaining of issues in GWT when handling dates. I am still very unclear as to what is "wrong" with the date handling, what special considerations need to be made, and when/where/why methods like Date.setMinutes(int minutes) should not be used.

Does anybody have feedback?

benstpierre
  • 32,833
  • 51
  • 177
  • 288
  • Since there is no Calendar class, a lot of the problems are the ones that arose in "old" Java before the implementation of the Calendar class, and are now back with GWT. I read great advice here: http://blog.gerardin.info/archives/674 - always transfer the dates logic to the server side, and keep it as simple as possible on the client. Hope someone will be able to make a more detailed explanation. – igorbel Oct 07 '10 at 21:50
  • And the Calendar class is implemented correctly...? I mean it is useful and easy to use? – Paweł Dyda Oct 08 '10 at 07:41
  • @Paweł: it has the advantage that it's not shown as deprecated ;) But I agree that it definitely could be easier to use. – Igor Klimer Oct 08 '10 at 10:10
  • I too think the Calendar class is far from perfect. In any case, there are more options and solutions for dealing with dates on the Server side. Actually, currently I cannot think of a single case where you couldn't transfer the date logic to the server? – igorbel Oct 08 '10 at 12:05
  • 3
    @igorbel: I can. Something like "15 minutes ago". If it is going to be updated continuously, it wouldn't make much sense to call server side function for every second and then for every minute or hour. It is better to do it with client-side script. – Paweł Dyda Oct 09 '10 at 17:07

1 Answers1

3

Way back in the beginning of Java (i.e., Java 1.0), the date/time api largely (solely?) consisted of the Date class. The Java folks were aware that it lacked robustness, so they added the Calendar class in Java 1.1 and tried to change the Date class into a value object by deprecating most of it. Unfortunately, the Calendar class itself wasn't very well thought-out (see here) and we're stuck with what many consider to be a monstrosity.

Bringing us up to today, GWT supports Date because, well... how can you live without dates?, but doesn't support Calendar/GregorianCalendar/TimeZone because it's so ugly and surely there has to be a better answer. Sadly, no one's thought of it in over 3 years as Calendar support was requested way back in January of 2007 and marked as planned by April 2008.

In short, go ahead and use the the deprecated Date methods in your GWT code, if that will work for what you need to do.

EDIT: GWT's CalendarUtil class might also come in handy.

Community
  • 1
  • 1
Tony
  • 444
  • 2
  • 4
  • 13
  • 1
    Well, it is extremely hard to get it right. Date & Time problems are quite complex. So many people tried and failed. Date class is at least UTC-based, we wouldn't be very happy to have something like .Net's DateTime structure (although its API is quite nice). – Paweł Dyda Oct 09 '10 at 17:12
  • 1
    I have no doubt that it's extremely hard to get right. I listened to a podcast once where one of the Java api guys was going on about how hard it was to encapsulate the abstract notion of a calendar. He gave an example (IIRC) of the Thai having something like ten different calendars, where each has a different number of days in the week. It sounds like a very hard problem. – Tony Oct 11 '10 at 16:03