Displaying date/time in GMT

378 views
Skip to first unread message

amye93

unread,
Mar 1, 2007, 12:58:54 PM3/1/07
to Google Web Toolkit
Hi, I have a situation where I need to display the date/time to the
user in GMT, regardless of the user's locale. I know in Java I can use
a SimpleDateFormat object with the time zone set to GMT. Is there
similar functionality in GWT? I have the gwt-widgets jar, which
includes a SimpleDateFormat object, but it doesn't look like it
supports time zones. I guess I can always format the date server-side
and set it as a String field in my serializable object, but I was
hoping to be able to format client-side to avoid adding an extraneous
field to my domain object, since it is only needed by GWT and not by
any other clients (i.e. clients calling web services, etc.) Any help
would be appreciated. Thanks!!

Amy

Dimitri Kurashvili

unread,
Mar 1, 2007, 1:51:48 PM3/1/07
to Google Web Toolkit
You can not use type java.util.Date (or java.util.Calendar) in your
GWT code. Therefore you can not format it.

amye93

unread,
Mar 1, 2007, 2:02:21 PM3/1/07
to Google Web Toolkit
Actually, I have used Date and the gwt-widgets SimpleDateFormat in
some of my GWT code already, so I know that works. Here is a sample:

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm");
table.setText(rowCount, COL_DATE, df.format(myDate));

How about if I use the date.getTimeZoneOffset() method, and manually
adjust the date to the GMT time before formatting it? Does this sound
reasonable? Something like:

long millis = myDate.getTime();
long offsetInMillis = myDate.getTimeZoneOffset() * 60000;
millis += offsetInMillis;
table.setText(rowCount, COL_DATE, df.format(new Date(millis)));

Are there any potential problems with this approach?

Dimitri Kurashvili

unread,
Mar 1, 2007, 2:03:34 PM3/1/07
to Google Web Toolkit
...
i mean using standard APIs. You should look for some special classes,
maybe even SimpleDateFormat (I do not know it) or implementing Date
and it's format by youself.

Dimitri Kurashvili

unread,
Mar 1, 2007, 2:16:34 PM3/1/07
to Google Web Toolkit
Do you mean java.util.Date and java.text.SimpleDateFormat?
What about me, i can not run them on GWT client side.

But if you really use java.text.SimpleDateFormat then try
===================
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm z");
or
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm z Z");
===================
to display time-zone.

Dimitri Kurashvili

unread,
Mar 1, 2007, 3:02:15 PM3/1/07
to Google Web Toolkit

>
> long millis = myDate.getTime();
> long offsetInMillis = myDate.getTimeZoneOffset() * 60000;
> millis += offsetInMillis;
> table.setText(rowCount, COL_DATE, df.format(new Date(millis)));
>

I think this is Ok (only use 60*60*1000 instead of 60000). But isn't
it better to use Calendar?

=============
.................
Calendar cal = Calendar.newInstance();
cal.setTime(yourDate);
cal.set(Calendar.ZONE_OFFSET, 60 * 60 * 1000 * zoneOffsetInHours);
yourDate = cal.getTime();
...........

=============

What the version of GWT are you using? I try once again use Date in
GWT widget but i fail. May be this is because i'm using version 1.2.22?

amye93

unread,
Mar 1, 2007, 3:24:14 PM3/1/07
to Google Web Toolkit
Sorry to be so unclear - we are using GWT 1.3.3. In my current code, I
am using java.util.Date and
org.gwtwidgets.client.util.SimpleDateFormat. The class where I use
these items is compiled into Javascript and used on the client side.
The current version of GWT does have support for java.util.Date, so
you should be able to use Date in your client-side code. I'm not sure
if this was implemented in version 1.2.22 or not though... GWT doesn't
have built in support for java.text.SimpleDateFormat or
java.util.Calendar, so for text formatting of dates I am using the
SimpleDateFormat class from the GWT Widget library available on
SourceForge:

http://sourceforge.net/projects/gwt-widget

This SimpleDateFormat class is very basic and doesn't have support for
setting the time zone like java.text.SimpleDateFormat does.

> I think this is Ok (only use 60*60*1000 instead of 60000)

In response to this, I agree your version is more readable. However
the extra 60 is not needed, because the method actually returns the
offset in minutes, not hours.

Thanks a lot for your suggestions - I will keep working! I'm not
really satisfied with my code above, even though it appears to work,
because it uses getTimezoneOffset(), which is deprecated so I get
compiler warnings. I would prefer to stay away from this if possible.

Dimitri Kurashvili

unread,
Mar 1, 2007, 3:46:44 PM3/1/07
to Google Web Toolkit
> > I think this is Ok (only use 60*60*1000 instead of 60000)
>
> In response to this, I agree your version is more readable. However
> the extra 60 is not needed, because the method actually returns the
> offset in minutes, not hours.

sorry, you are right. i mix it with Calendar timezone offset (in
millis).

Graham H

unread,
Mar 1, 2007, 7:35:27 PM3/1/07
to Google Web Toolkit
I had the same problem.
There is a GWT issue that has been raised for this already (issue
#398).

To get around this problem in my project I ended up hacking the GWT
date serialization so that it creates the date instance using the time/
date components rather than the getTime() call.

See below for my hacked version.

(Note this is probably not a solution for everyone as there will be
cases where the timezone is required.)

g
h

/*
* Hacked version of
com.google.gwt.user.client.rpc.core.java.util.Date_CustomFieldSerializer
*/
public final class Date_CustomFieldSerializer {

public static void deserialize(SerializationStreamReader
streamReader,
Date instance) {
// No fields
}

public static Date instantiate(SerializationStreamReader
streamReader)
throws SerializationException {
return new
Date(streamReader.readInt(),streamReader.readInt(),streamReader.readInt(),streamReader.readInt(),streamReader.readInt());
}

public static void serialize(SerializationStreamWriter streamWriter,
Date instance) throws SerializationException {
streamWriter.writeInt(instance.getYear());
streamWriter.writeInt(instance.getMonth());
streamWriter.writeInt(instance.getDate());
streamWriter.writeInt(instance.getHours());
streamWriter.writeInt(instance.getMinutes());

Reinier Zwitserloot

unread,
Mar 1, 2007, 7:54:37 PM3/1/07
to Google Web Toolkit
I recall there being some sort of formatter included in the library
upgrades of GWT 1.4.

Speaking of 1.4... wasn't there some sort of feature demo release
planned around, well, now?

Looking forward to it, 1.4 has some seriously cool new features.

Ed Coyne

unread,
Mar 2, 2007, 11:14:21 AM3/2/07
to Google Web Toolkit
To take this conversation in a different direction.

Unless GWT does a transparent server call behind the scenes any
Clientside date/time will be subject to the client's clock. Thus it
would be unreliable. Personally I would just make a rpc request for
the time from the server so I could trust it.

Ed

Reply all
Reply to author
Forward
0 new messages