2 Replies Latest reply: May 3, 2010 1:44 AM by 791266 RSS

    Date's value changed after transfered it to different time zone

    843790
      The client (Java stand application) try to send an date to web server via http protocol (HttpURLConnection), and they are located at different time zone like three hours, from client send one date (like Mar 02, 2010 *00:00:00*)

      outputStream.writeObject(date);


      and server receive this date: Date date = (Date)objectInputStream.readObject();

      but its date become Mar 01, 2010 *21:00:00*, also three hour difference, may I know what happened? how do I prevent this happened?


      Thanks
        • 1. Re: Date's value changed after transfered it to different time zone
          EJP
          That's correct. The date retained its absolute value. Its original value at the original location was midnight. At the new location it is three hours earlier, because the new location is three hours later.
          • 2. Re: Date's value changed after transfered it to different time zone
            791266
            ewterwytryu wrote:
            but its date become Mar 01, 2010 *21:00:00*, also three hour difference, may I know what happened? how do I prevent this happened?
            The Data class in java is a wrapper around a millisecond value that always represents a time in UTC (GMT). That millisecond value can then be displayed in many different formats, and the normal toString() on a date returns the milliseconds formatted into a string with timezone information. That timezone information is taken from the machine that is executing toString().

            Use a SimpleDateFormat if you want to create a string representation that represents another timezone (or format).

            Nothing of this is related to Serialization, and it's all documented in the javadoc for Date.