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

GregorianCalendar.clone() doesn't create separate objects

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1
    • core-libs
    • None
    • sparc
    • solaris_2.5.1



      Name: mc57594 Date: 02/07/97


      If you clone a GregorianCalendar, you get references to the
      same object. I you modify one, all of them get modified:
      import java.io.*;
      import java.util.*;
      class Test {
        public static void main(String[] args) {
          String s;
          
          GregorianCalendar d1, d2, d3;
          d1 = new GregorianCalendar(1997,1,16);
          d2 = (GregorianCalendar) d1.clone();
          d3 = (GregorianCalendar) d1.clone();
          d1.set(Calendar.HOUR, 9);
          d2.set(Calendar.HOUR, 10);
          d3.set(Calendar.HOUR, 11);
          System.out.println(d1.getTime().toString() +
                             "\n" + d2.getTime().toString() +
                             "\n" + d3.getTime().toString());
        }
      }

      hoth% java Test
      Sun Feb 16 11:00:00 GMT+03:00 1997
      Sun Feb 16 11:00:00 GMT+03:00 1997
      Sun Feb 16 11:00:00 GMT+03:00 1997
      hoth%

      company - Sun Microsystems , email - ###@###.###
      ======================================================================

            pbk Peter Kessler
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: