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

Serialized GregorianCalendar does not perform set(...) correctly

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 1.3.1_08
    • 1.2.2, 1.3.0, 1.3.1_06
    • core-libs
    • 08
    • generic, x86, sparc
    • generic, solaris_8, windows_nt

    Backports

      Description



        Name: stC104175 Date: 02/25/2000


        java version "1.2.2"
        Classic VM (build JDK-1.2.2-W, green threads, sunwjit)

        A GregorianCalendar that has been obtained by de-serialization (as in RMI) does
        not handle the Calendar.set(int field, int value) method correctly. When this
        method is used, some fields get reset to a date somewhere in 1970.

        The following program creates 2 GregorianCalendar objects one is created with
        new, the other is created by serializing and deserializing the first. Calling
        the same set(...) method on these two objects demonstrates the problem. Only
        the DATE field should be changed, but the YEAR and other fields are lost/set to
        strange values.

        This is a very nasty bug since it will usually only show up in RMI/EJB
        applications. (VERY hard to reproduce!)

        ---- Start CalBug.java ----
        import java.util.Calendar;
        import java.util.GregorianCalendar;
        import java.io.*;

        public class CalBug
        {
            public static void main(String[] args)
            {
                Calendar a = new GregorianCalendar();

                Calendar b = null;
                try {
                    ObjectOutputStream out = new ObjectOutputStream(new
        FileOutputStream("CalBug.ser"));
                    out.writeObject(a);
                    out.close();

                    ObjectInputStream in = new ObjectInputStream(new
        FileInputStream("CalBug.ser"));
                    b = (Calendar)in.readObject();
                    in.close();
                } catch (ClassNotFoundException cnfe) {
                    cnfe.printStackTrace();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }

                System.out.println("before a.set(Calendar.DATE, 16): "+a.getTime());
                a.set(Calendar.DATE, 16);
                System.out.println("after a.set(Calendar.DATE, 16): "+a.getTime());

                // Workaround: Uncomment the following line to work around bug
                // b.setTime(b.getTime());

                System.out.println("before b.set(Calendar.DATE, 16): "+b.getTime());
                b.set(Calendar.DATE, 16);
                System.out.println("after b.set(Calendar.DATE, 16): "+b.getTime());
            }
        }
        ---- End CalBug.java ----
        (Review ID: 101637)
        ======================================================================

        Attachments

          Issue Links

            Activity

              People

                peytoia Yuka Kamiya (Inactive)
                stompkinsunw Sean Tompkins (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: