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

java.sql.Date compareTo method seems necessary

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Future Project
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • windows_xp

      Name: rmT116609 Date: 03/03/2004


      A DESCRIPTION OF THE REQUEST :
      Shouldn't java.sql.Date override the java.util.Date compareTo methods? The following example class and output shows inconsistent behavior when comparing two java.sql.Date objects. One would expect that when comparing two java.sql.Date objects whose month, date, and year are the same that the compareTo method would return 0, indicating that the dates are equal. The code below, however, shows (as documented in the javadoc, that the base class, java.util.Date compareTo method is being used instead).

      JUSTIFICATION :
      As written in the javadoc, "the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated", and it seems consistent to me that date comparisons between two java.sql.Date objects would likewise ignore the time values.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When comparing to java.sql.Date objects whose month, date, and year values are equal, the compareTo method should return 0, indicating that the dates are equal.
      ACTUAL -
      The actual behavior is that the hour, minutes, seconds, and millisecond values are also used in the comparison.

      ---------- BEGIN SOURCE ----------
      /*
       * JavaSqlDateCompareProblem.java
       *
       * Created on December 29, 2003, 1:30 PM
       */

      import java.util.Calendar;

      public class JavaSqlDateCompareProblem {
        
        /** Creates a new instance of JavaSqlDateCompareProblem */
        public JavaSqlDateCompareProblem() {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
          
          // Retain the current time
          Calendar todayCal = Calendar.getInstance();

          // Create a new calendar and set it to the current month, date, year
          Calendar todaySqlCal = Calendar.getInstance();
          todaySqlCal.clear(); // Clear all time fields
          todaySqlCal.set( Calendar.MONTH, todayCal.get( Calendar.MONTH ) );
          todaySqlCal.set( Calendar.DATE, todayCal.get( Calendar.DATE ) );
          todaySqlCal.set( Calendar.YEAR, todayCal.get( Calendar.YEAR ) );
          // Create a new java.sql.Date based on the current month, date, year
          java.sql.Date todaySqlDate = new java.sql.Date( todaySqlCal.getTime().getTime() );

          System.out.println( "Today's SQL Date = " + todaySqlDate );
          
          // Create a new java.sql.Date based on the current system time
          java.sql.Date nowSqlDate = new java.sql.Date( System.currentTimeMillis() );
          
          System.out.println( "Current SQL Date = " + nowSqlDate );
          
          System.out.println( "Today's SQL Date == Current SQL Date? " + ( todaySqlDate.compareTo( nowSqlDate ) == 0 ) );
          
          System.out.println( System.getProperties() );
        }
        
      }


      =====================================
      Output:

        Today's SQL Date = 2003-12-29
      Current SQL Date = 2003-12-29
        Today's SQL Date == Current SQL Date? false

      {java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=C:\Java\j2sdk1.4.2\jre\bin, java.vm.version=1.4.2-b28, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=US, sun.os.patch.level=Service Pack 1, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\Java\NetBeans IDE 3.5, java.runtime.version=1.4.2-b28, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\Java\j2sdk1.4.2\jre\lib\endorsed, os.arch=x86, line.separator=
      , java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, os.name=Windows XP, sun.java2d.fontpath=, java.specification.name=Java Platform API Specification, java.class.version=48.0, java.util.prefs.PreferencesFactory=java.util.prefs.WindowsPreferencesFactory, os.version=5.1, user.timezone=GMT-05:00, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.4, user.name=s011974, java.vm.specification.version=1.0, java.home=C:\Java\j2sdk1.4.2\jre, sun.arch.data.model=32, user.language=en, java.specification.vendor=Sun Microsystems Inc., awt.toolkit=sun.awt.windows.WToolkit, java.vm.info=mixed mode, java.version=1.4.2, file.separator=\, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=pentium i486 i386}
      ^C
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround is to create Calendar objects based on the java.sql.Date objects, clear the hour, minute, second, and millisecond fields and then compare the dates.
      (Incident Review ID: 232387)
      ======================================================================

            lancea Lance Andersen
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: