How can I format a date to a specific timezone?

113 views
Skip to first unread message

Kidahl

unread,
Oct 10, 2007, 7:01:13 AM10/10/07
to Google Web Toolkit

I would like to output a given date formatted in GMT.

However, I cannot find a way to control the timezone the
DateTimeFormat class uses when formatting dates:

On my client:

DateTimeFormat fmt = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm:ss
z");

fmt.format(myDate) --> 11.08.1973 01:00:00 GMT+01:00

How can I format the date so that I get
11.08.1973 00:00:00 GMT+00:00 instead??

The dateTimeFormat class obviously supports timezones, but how can I
control it?

Regards,
Karl Ivar Dahl

Peter Blazejewicz

unread,
Oct 11, 2007, 9:17:37 PM10/11/07
to Google Web Toolkit
hi Karl,
Iv'e looked into sources and it will always append current Time Zone
offset (something not configurable from GWT) to your date if you are
using "z" or "Z",
so just try:

DateTimeFormat fmt = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm:ss");
label.setText(fmt.format(d) + " GMT+00:00");

regards,
Peter

Kidahl

unread,
Oct 17, 2007, 8:11:51 AM10/17/07
to Google Web Toolkit
Tanks for your reply Peter!

I have tested this now, and it turns out that while removing the "z"
removes the "GMT+01:00" part of the output, the date is still
presented in GMT+1 (one hour off).

DateTimeFormat fmt = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm:ss");

fmt.format(myDate) --> 11.08.1973 01:00:00

The incoming date is actually 11.08.1973 00:00:00 GMT+0

There must be a way to tell the formatter that it should present the
date in GMT+0?

Does anyone know how GWT aquires the client timezone used for this
formatting?

-Karl Ivar

On Oct 12, 3:17 am, Peter Blazejewicz <peter.blazejew...@gmail.com>
wrote:

Peter Blazejewicz

unread,
Oct 17, 2007, 9:25:38 AM10/17/07
to Google Web Toolkit
hi Karl,
you should look into sources (via SVN that should be simple),
I think it gets client local time,
if I run such code i see that output formats from GWT Date
implementation are slightly different from native, but timezone offset
is used:

package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

import java.util.Date;

public class TestDateModule implements EntryPoint {
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
Date d = new Date();
// gwt
rootPanel.add(new Label("GWT date object"));
rootPanel.add(new Label("toLocaleString: " + d.toLocaleString()));
rootPanel.add(new Label("toGMTString: " + d.toGMTString()));
rootPanel.add(new Label("toString: " + d.toString()));
// native
JavaScriptObject jsd = getDate();
rootPanel.add(new Label("JavaScript object"));
rootPanel.add(new Label("toLocaleString: " +
dateToLocaleString(jsd)));
rootPanel.add(new Label("toGMTString: " + dateToGMTString(jsd)));
rootPanel.add(new Label("toUTCString: " + dateToUTCString(jsd)));
rootPanel.add(new Label("toString: " + dateToString(jsd)));
}

private static native JavaScriptObject getDate()/*-{
return new Date();
}-*/;

private static native String dateToLocaleString(JavaScriptObject
date)/*-{
return date.toLocaleString();
}-*/;

private static native String dateToGMTString(JavaScriptObject date)/
*-{
return date.toGMTString();
}-*/;

private static native String dateToUTCString(JavaScriptObject date)/
*-{
return date.toUTCString();
}-*/;

private static native String dateToString(JavaScriptObject date)/*-{
return date.toString();
}-*/;
}

GWT date object
toLocaleString: 2007-10-17 15:20:05
toGMTString: 17 Oct 2007 13:20:05 GMT
toString: Wed Oct 17 15:20:05 CEST 2007
JavaScript object
toLocaleString: 17 October 2007 15:20:05
toGMTString: Wed, 17 Oct 2007 13:20:05 UTC
toUTCString: Wed, 17 Oct 2007 13:20:05 UTC
toString: Wed Oct 17 15:20:05 UTC+0200 2007

I guess GWT formatter takes offset from native object somehow and show
it in java compatible way,

regards,
Peter

Reply all
Reply to author
Forward
0 new messages