JDK-4340146 : Calendar.equals modifies state
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2000-05-22
  • Updated: 2003-11-05
  • Resolved: 2003-11-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b28Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
The following program shows that Calendar.equals changes the observable state of the calendar. The output I got is:
Date with equals:    Tue May 30 20:25:08 PDT 2000
Date without equals: Sun Apr 30 20:25:08 PDT 2000
Calling a.equals(b) should never, ever have any observable effect on a or b.


import java.util.Calendar;

public class CalEquals {

    public static void main(String[] args) {
        Calendar cal1 = makeCalendar();
        Calendar cal2 = makeCalendar();
        Calendar cal3 = makeCalendar();
        
        cal1.equals(cal2);
        cal1.set(Calendar.DAY_OF_MONTH, 30);
        cal3.set(Calendar.DAY_OF_MONTH, 30);
        System.out.println("Date with equals:    " + cal1.getTime());
        System.out.println("Date without equals: " + cal3.getTime());
    }
    
    private static Calendar makeCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.APRIL);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        return cal;
    }
}


The Date class has the same problem since JDK1.0.
###@###.### 2002-08-05

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b28 tiger-beta
14-06-2004

EVALUATION equals() calls getTimeInMillis() which performs normalization of the date fields. Date.compareTo() has the same problem. Possibly there are more methods. Another version of getTimeInMillis() that returns UTC time without modifying the date fields may be required for those methods. masayoshi.okutsu@Eng 2000-06-13 It's very likely that there are some applications which rely on the side effect. This bug will not be fixed due to the compatibility concerns. ###@###.### 2002-08-05 Realized that both equals() and hashCode() must be fixed to be used as a key with the Collection API. Reopening this and the related hashCode bug. ###@###.### 2002-08-27
27-08-2002