tags:

views:

383

answers:

2

Since GWT does not provide the GregorianCalendar class, how to do calendar operations on the client?

I have a Date a and I want the Date, which is n days after a.

Examples:

a (2000-01-01) + n (1) -> 2000-01-02
a (2000-01-01) + n (31) -> 2000-02-01
+1  A: 

The answer that Google seems to use (currently), is:

  @SuppressWarnings("deprecation") // GWT requires Date
  public static void addDaysToDate(Date date, int days) {
    date.setDate(date.getDate() + days);
  }

This is from the class com.google.gwt.user.datepicker.client.CalendarUtil, which is used by com.google.gwt.user.datepicker.client.DatePicker. I imagine, that there will be problems involved, when doing calculations in different timezones.

Lots of people have already voted for some kind of Joda time for GWT: http://code.google.com/p/google-web-toolkit/issues/detail?id=603 . The currently last comment states, that there's a new fork of goda time, maybe we should really check it out.

Chris Lercher
+1  A: 

I've created a rough implementation that emulates TimeZone, Calendar, and Locale. Feel free to try it out here:

http://code.google.com/p/gwt-calendar-class/downloads/list