Amy
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?
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.
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?
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.
sorry, you are right. i mix it with Calendar timezone offset (in
millis).
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());
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.
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