JDK-4031014 : GregorianCalendar.clone() doesn't create separate objects
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 1.1
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1997-02-07
  • Updated: 1997-02-07
  • Resolved: 1997-02-07
Related Reports
Duplicate :  
Relates :  
Relates :  
Description

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  -  ###@###.###
======================================================================

Comments
WORK AROUND Name: mc57594 Date: 02/07/97 Use of GregorianCalendar.getTime().toString() can change the way it works : 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); s = d1.getTime().toString(); d2.set(Calendar.HOUR, 10); s = d2.getTime().toString(); 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 09:00:00 GMT+03:00 1997 Sun Feb 16 10:00:00 GMT+03:00 1997 Sun Feb 16 11:00:00 GMT+03:00 1997 hoth% ======================================================================
11-06-2004

PUBLIC COMMENTS GregorianCalendar.clone() doesn't create separate objects.
10-06-2004

EVALUATION This has been fixed as #4026300 (40298908 and 4028518 are other duplicates). Using the latest JDK1.1, I get: % cat java/B4031014.java import java.io.*; import java.util.*; class B4031014 { 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()); } } % /usr/local/java/jdk1.1/solaris/bin/javac -d classes -classpath classes:/usr/local/java/jdk1.1/solaris/lib/classes.zip java/B4031014.java % /usr/local/java/jdk1.1/solaris/bin/java -classpath classes:/usr/local/java/jdk1.1/solaris/lib/classes.zip B4031014 Sun Feb 16 09:00:00 PST 1997 Sun Feb 16 10:00:00 PST 1997 Sun Feb 16 11:00:00 PST 1997 So I'm closing this as not a bug. peter.kessler@Eng 1997-02-07
07-02-1997