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

Calling get(...) / getTime() on a Calendar instance makes isSet(...) useless!

XMLWordPrintable

    • b22
    • x86
    • linux



        Name: rmT116609 Date: 04/21/2003


        FULL PRODUCT VERSION :
        java version "1.4.2-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b19)
        Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)
           and (see text)
        java version "1.4.1_01"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
        Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

        FULL OS VERSION :
        Linux 2.4.18-4GB i686 unknown

        A DESCRIPTION OF THE PROBLEM :
        The result of .isSet(int field) is incorrect after .get(int field) was called. With 1.4.2 beta even calling .getTime() affects successive results of .isSet(int field). The workaround for 1.4.1 doesn't work for 1.4.2 beta.
        By this, the method isSet(int) and in some contexts the whole class does not work completely.
        For details please see the attached test program.



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile and start the attached test program.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        Test I (without workaround)
                  First call .toString: 2003
                 Second call .toString: 2003
        Test II (with workaround)
                  First call .toString: 2003
                 Second call .toString: 2003
        Please note that the results differ between 1.4.1 and 1.4.2 beta!

        1.4.1:
        Test I (without workaround)
                  First call .toString: 2003
                 Second call .toString: 01.01.2003
        Test II (with workaround)
                  First call .toString: 2003
                 Second call .toString: 2003

        1.4.2 beta:
        Test I (without workaround)
                  First call .toString: 2003
                 Second call .toString: 01.01.2003
        Test II (with workaround)
                  First call .toString: 2003
                 Second call .toString: Jan 2003

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.text.*;
        import java.util.Calendar;

        public class TestUtilCalendar {
            Calendar calendar = Calendar.getInstance();
            private boolean workaround;

            public TestUtilCalendar(boolean workaround) {
                this.workaround = workaround;
                calendar.clear();
                calendar.set(Calendar.YEAR, 2003);
            }

            public String toString() {
                if (!calendar.isSet(Calendar.DAY_OF_MONTH) && !calendar.isSet(Calendar.MONTH)) { // Should always be true
                    if (!workaround) {
                        return Integer.toString(calendar.get(Calendar.YEAR));
                    } else {
                        // Workaround
                        String str = new SimpleDateFormat("yyyy").format(calendar.getTime());
                        while (str.charAt(0) == '0') str = str.substring(1);
                        return str;
                    }
                } else if(!calendar.isSet(Calendar.DAY_OF_MONTH)) { // Should never be called
                    return new SimpleDateFormat("MMM yyyy").format(calendar.getTime());
                } else { // Should never be called
                    return DateFormat.getDateInstance().format(calendar.getTime());
                }
            }

            public static void main(String args[]) {
                System.out.println("Test I (without workaround)");
                TestUtilCalendar calendar = new TestUtilCalendar(false);
                System.out.println(" First call .toString: " + calendar.toString());
                System.out.println(" Second call .toString: " + calendar.toString());

                System.out.println("Test II (with workaround)");
                calendar = new TestUtilCalendar(true);
                System.out.println(" First call .toString: " + calendar.toString());
                System.out.println(" Second call .toString: " + calendar.toString());
            }

        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        For 1.4.1:
          Never ever call .get(int field) when you need to rely on isSet(int field) to return correct values. (see souce code for test case)

        For 1.4.2 beta:
          The workaround above does not work here
        (Review ID: 184541)
        ======================================================================

              okutsu Masayoshi Okutsu
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: