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

SpinnerDateModel accepts Calendar objects at compile time but not at runtime

XMLWordPrintable

      FULL PRODUCT VERSION :
      1.6.0_07-b06

      ADDITIONAL OS VERSION INFORMATION :
      WinXP SP2

      A DESCRIPTION OF THE PROBLEM :
      While Calendar objects are accepted by the compiler in the constructor of SpinnerDateModel as "Comparable", a ClassCastException is thrown at runtime.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No runtime error.
      ACTUAL -
        Program aborted

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Date cannot be cast to java.util.Calendar
              at java.util.Calendar.compareTo(Unknown Source)
              at javax.swing.SpinnerDateModel.<init>(Unknown Source)
              at JSpinnerDemo2.<init>(JSpinnerDemo2.java:19)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.util.*;
      import javax.swing.*;

      public class JSpinnerDemo extends JFrame {

        public JSpinnerDemo() {
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          Date ini= new Date(System.currentTimeMillis()-2592000000l); // -30 days
          Calendar start= Calendar.getInstance();
          start.set(1445, 0, 1);
          Calendar now= Calendar.getInstance();
          SpinnerDateModel spinModel= new SpinnerDateModel
      (ini,start,now,Calendar.MONTH);
      // ClassCastException: java.util.Date cannot be cast to java.util.Calendar

          JSpinner spinAcqDate = new JSpinner(spinModel);
          add(spinAcqDate);
          pack();
          setVisible(true);
        }

        public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
            public void run() {
      new JSpinnerDemo();
            }
          });
        }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Use Date instead of Calendar as parameters (Thanks to Camickr):

         SpinnerDateModel spinModel= new SpinnerDateModel
      (ini,start.getTime(),now.getTime(),Calendar.MONTH);

            tr Tejesh R
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: