If you're using RPC, do a subclass of Date:
public class LocalDate extends Date {
public LocalDate() {
super();
}
public LocalDate(Date date) {
this(date.getTime());
}
@SuppressWarnings("deprecation")
public LocalDate(int year,
int month,
int date) {
super(year,
month,
date);
}
public LocalDate(Long date) {
super(date);
}
}
and then make a custom serializer for it:
public final class LocalDate_CustomFieldSerializer extends
CustomFieldSerializer<LocalDate> {
public static void
deserialize(com.google.gwt.user.client.rpc.SerializationStreamReader reader,
com.practiware.types.LocalDate instance) {
}
public static LocalDate instantiate(SerializationStreamReader streamReader)
throws SerializationException {
try {
int year = streamReader.readInt();
int month = streamReader.readInt();
int day = streamReader.readInt();
return new LocalDate(year,
month,
day);
} catch (Throwable e) {
throw new SerializationException(e);
}
}
@SuppressWarnings("deprecation")
public static void
serialize(com.google.gwt.user.client.rpc.SerializationStreamWriter
streamWriter,
com.practiware.types.LocalDate instance)
throws SerializationException {
try {
int month = instance.getMonth();
int year = instance.getYear();
int day = instance.getDate();
streamWriter.writeInt(year);
streamWriter.writeInt(month);
streamWriter.writeInt(day);
} catch (Exception e) {
throw new SerializationException(e);
}
}
@Override
public void deserializeInstance(SerializationStreamReader streamReader,
LocalDate instance) throws SerializationException {
LocalDate_CustomFieldSerializer.deserialize(streamReader,
instance);
}
@Override
public boolean hasCustomInstantiateInstance() {
return true;
}
@Override
public LocalDate instantiateInstance(SerializationStreamReader
streamReader) throws SerializationException {
return LocalDate_CustomFieldSerializer.instantiate(streamReader);
}
@Override
public void serializeInstance(SerializationStreamWriter streamWriter,
LocalDate instance) throws SerializationException {
LocalDate_CustomFieldSerializer.serialize(streamWriter,
instance);
}
}
You can use a regular Date in this case for getters/setters, but then wrap
it into a LocalDate in the setter. Making a subclass will allow you to
keep other dates where you have a time inside the date object that you
might want to be converted to the user's local time.
If you're using RequestFactory, you'll want to have it converted to UTC or
whatever time zone your server is on before sending it across in the date
field. If you're using DateBox, there's an example of one that converts to
UTC
here:
http://gwt-traction.googlecode.com/hg/demo/com.tractionsoftware.gwt.demo.utcdatebox.UTCDateBoxDemo/UTCDateBoxDemo.html
On Thursday, June 5, 2014 2:41:08 AM UTC-5, Mayank pandya wrote:
>
> Hi
>
> From few days I am fighting with different Timezone issues of server and
> GWT client. but cannot get any success.
>
> Scenario is Server is in UTC timezone let say Client A is in IST timezone.
> When client select a date (with time) I pushed to server but date is
> automatically changed to server's timezone. I dig around this issue and I
> found multiple solutions like
>
> 1. create custom serializer (No idea how to do that can't found any proper
> example)
> 2. transfer date as a string to server and convert it to server timezone
> and store it. and when fetching data convert again from server's timezone
> to client local timezone. sounds good idea.
>
> So my query is.
>
> 1. Any other solutions?
> 2. which is best way to manage this ?
> 3. any sample code or link?
>
>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.