Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4328747

Serialization appears to break Calendar objects

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.2
    • core-libs
    • beta
    • generic
    • generic



      Name: stC104175 Date: 04/07/2000


      java version "1.2.2"
      Classic VM (build 1.2.2-L, green threads, nojit)

      Serialization of a Calendar instance can give problems and changing dates.
      The following code snippet illustrates the problem.

      The line
            result.setTime(result.getTime());
      does not occur in my application, but was the simplest I could find that shows
      broken behaviour.


        public static void main(String args[]) {
          try {
            Calendar c = Calendar.getInstance();
            c.set(1966,0,1); // 1 jan 1966
            System.out.println(c.get(c.YEAR)+" "+c.get(c.MONTH)+" "+c.get(c.DATE));
             // Gives 1966 0 1


            //serialize ...
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream s = new ObjectOutputStream(out);
            s.writeObject(c);
            s.flush();
            // deserialize ...
            ObjectInputStream t = new ObjectInputStream(new
      ByteArrayInputStream(out.toByteArray()));
            Calendar result = (Calendar)t.readObject();
            System.out.println(result.get(result.YEAR)+" "+result.get(result.MONTH)+"
      "+result.get(result.DATE));
             // Gives 1966 0 1
            result.setTime(result.getTime());
            System.out.println(result.get(result.YEAR)+" "+result.get(result.MONTH)+"
      "+result.get(result.DATE));
             // Gives 1965 11 19 !!!!!!!!!

          }
          catch (Exception e){}
        }
      (Review ID: 103427)
      ======================================================================

            okutsu Masayoshi Okutsu
            stompkinsunw Sean Tompkins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: