Thursday, June 25, 2009

If you're like me and you really love to use Google Web Toolkit, you would probably also have noticed that the API docs are not very good. It has taken me many hours just to figure out how to format the date value in the new DatePicker which recently migrated to GWT 1.6 from gwt incubator. So to any who stuble unto this blog here, what I've tried to do is lay out the solutions to all the problems that have plagued me with GWT;

1. How to format the DatePicker to display a date.
The API gives a lot of details about the different patterns to use, but I've found that using the pre-configured patterns is probably the easiest way to go gives a lot of details about the different patterns to use, but I've found that using the pre-configured patterns is probably the easiest way to go. For example:
...

    Date today = new Date();

// prints Tue Dec 18 12:01:26 GMT-500 2007 in the default locale.
GWT.log(today.toString(), null);

// prints 12/18/07 in the default locale
GWT.log(DateTimeFormat.getShortDateFormat().format(today), null);

// prints December 18, 2007 in the default locale
GWT.log(DateTimeFormat.getLongDateFormat().format(today), null);

// prints 12:01 PM in the default locale
GWT.log(DateTimeFormat.getShortTimeFormat().format(today), null);

// prints 12:01:26 PM GMT-05:00 in the default locale
GWT.log(DateTimeFormat.getLongTimeFormat().format(today), null);

// prints Dec 18, 2007 12:01:26 PM in the default locale
GWT.log(DateTimeFormat.getMediumDateTimeFormat().format(today), null);

// A custom date format
DateTimeFormat fmt = DateTimeFormat.getFormat("EEEE, MMMM dd, yyyy");
// prints Monday, December 17, 2007 in the default locale
GWT.log(fmt.format(today), null);

So to format a DateBox to display a date like 06/26/2009
dateBox = new DateBox();
dateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getShortDateFormat()));
...

11 comments:

  1. Thank you! This was not trivial!

    ReplyDelete
  2. Thank you! The docs are completely not clear on this.

    ReplyDelete
  3. there are two formats 22/08/2010 and 22.08.2010, then how to ensure that any one of them has been entered???

    ReplyDelete
  4. Спасибо большое)!!

    ReplyDelete
  5. help me please!!!

    how can i do to change to previus year?

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. DateTimeFormat.getShortDateFormat() is depricated now !

    Use this form instead:

    DateTimeFormat.getFormat("yyyy/MM/dd")

    ReplyDelete