users
[Top] [All Lists]

Re: [cinjug-users] Date Serialization

To: Brian Bonner <brian.bonner@xxxxxxxxxxxx>
Subject: Re: [cinjug-users] Date Serialization
From: Eric Bardes <ericbardes@xxxxxxxxx>
Date: Tue, 17 May 2005 15:24:56 -0400
Cc: users@xxxxxxxxxx
Delivered-to: mailing list users@cinjug.org
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=rj1X8wmjuQGzEzWfTbUHOl0Hm6dKrm5zkrsQC2YPkQrqatmimxos2CdWqb9UoHomqQyg/Gh4jJ/xKq8+STJF9ABZ/sPt3sC24blwRHz29QoAmKiWU4k9Ert2kgVv2B6TEbO3xN+IShmJeTpMOfMgULnxb0a7rXEZ8kYRWkTV42Y=
In-reply-to: <428A38EE.9050103@paraware.com>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
References: <428A38EE.9050103@paraware.com>
Reply-to: Eric Bardes <ericbardes@xxxxxxxxx>
Strictly speaking, the time ISN'T shifting.  15:00 hours in New York
is the same time as 12:00 in Seattle.

POJO store time as # of ms since 1/1/70 0:00:00 UTC.  When converted
to a string, the JVM applies the default TimeZone which is based upon
the configuration of the host OS.

Trick 1:  Convert to String before serializing to client.
Trick 2:  Add TimeZone information to the serialized class.
Trick 3:  Hard-Code "GMT" or whatever into the applet.

Below is a code fragment which shows how the same Date object can
print two different times for two different timezones without changing
its value.

-----------------------------------------------------------------
import java.text.*;
import java.util.*;

public class z
{
  public static void main(String args[])
  {
    Date d = new Date();
    System.out.println(d.getTime());

    DateFormat df = DateFormat.getDateTimeInstance();

    df.setTimeZone(TimeZone.getTimeZone("EST5EDT"));
    System.out.println(df.format(d));

    df.setTimeZone(TimeZone.getTimeZone("PST8PDT"));
    System.out.println(df.format(d));

    System.out.println(d.getTime());
  }
}

$ java z
1116357599329
17-May-05 3:19:59 PM
17-May-05 12:19:59 PM
1116357599329

On 17/05/05, Brian Bonner <brian.bonner@xxxxxxxxxxxx> wrote:
> Guys, I have an app that does Java serialization of POJOs between a
> client and a server.  The POJOs contain a date which stores the time in
> ms since 1/1/70.   The app works fine with one exception -- the date of
> the pojo displayed in the applet in one time zone is different than the
> date of the pojo displayed in the applet from a different time zone.
> 
> I've talked w/ folks and can only attribute this to the timezone of the
> PC (or the JVM) being set incorrectly.  Does anyone else have any
> thoughts on what else might be causing this time shift?
> 
> Thanks.
> 
> --
> Brian
> 
> ---------
> You may unsubscribe from this mailing list
> by sending a blank email addressed to:
> users-unsubscribe@xxxxxxxxxx
> 
> --
> Find additional help by sending a blank email
> addressed to:
> users-help@xxxxxxxxxx
> 
> 



-- 
Cheers,
Eric Bardes

<Prev in Thread] Current Thread [Next in Thread>