United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6898593 java.sql.Date.valueOf no exception if date given is not in the JDBC date escape format(yyyy-mm-dd)
JDK-6898593 : java.sql.Date.valueOf no exception if date given is not in the JDBC date escape format(yyyy-mm-dd)

Details
Type:
Bug
Submit Date:
2009-11-05
Status:
Closed
Updated Date:
2012-10-01
Project Name:
JDK
Resolved Date:
2010-04-08
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.sql
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
6u16
Fixed Versions:
6u17-rev (b09)

Related Reports
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:
Relates:
Relates:
Relates:

Sub Tasks

Description
As per the API doc for java.sql.Date.valueOf, IllegalArgumentException should 
be thrown if the date given is not in the JDBC date escape format 
(yyyy-mm-dd). 
 
The error is not thrown when I enter the value '20009-10-10'. Instead it 
silently stores the value. But later when I do java.sql.toString() 
 
    public static void main(String[] args) { 
        java.sql.Date jsd = java.sql.Date.valueOf("20009-06-26"); 
        System.out.println(jsd); 
 
    } 



 
The above program prints 
*009-06-26 Where '*' is a non-readable character. 

We need fix in valueOf method() to throw the exception if we pass 5 digits to 
the API.

From API
valueOf

public static Date valueOf(String s)

    Converts a string in JDBC date escape format to a Date value.

    Parameters:
        s - a String object representing a date in in the format "yyyy-mm-dd" 
    Returns:
        a java.sql.Date object representing the given date 
    Throws:
        IllegalArgumentException - if the date given is not in the JDBC date escape format (yyyy-mm-dd)

                                    

Comments
EVALUATION

See comments
                                     
2009-11-10
SUGGESTED FIX

------- Date.java -------
*** /tmp/sccs.fraqIi    Tue Nov 10 11:09:19 2009
--- Date.java   Tue Nov 10 10:37:47 2009
***************
*** 85,109 ****
       *         JDBC date escape format (yyyy-mm-dd)
       */
      public static Date valueOf(String s) {
!       int year;
!       int month;
!       int day;
!       int firstDash;
!       int secondDash;

!       if (s == null) throw new java.lang.IllegalArgumentException();

!       firstDash = s.indexOf('-');
!       secondDash = s.indexOf('-', firstDash+1);
!       if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
!           year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
!           month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
!           day = Integer.parseInt(s.substring(secondDash+1));
!       } else {
!           throw new java.lang.IllegalArgumentException();
!       }
!
!       return new Date(year, month, day);
      }

      /**
--- 85,126 ----
       *         JDBC date escape format (yyyy-mm-dd)
       */
      public static Date valueOf(String s) {
!         final int YEAR_LENGTH = 4;
!         final int MONTH_LENGTH = 2;
!         final int DAY_LENGTH = 2;
!         int year = 0;
!         int month = 0;
!         int day  = 0;
!         int firstDash;
!         int secondDash;
!         String mm;
!         String dd;
!         String yyyy;
!         boolean parsedDate = false;

!         if (s == null) {
!             throw new java.lang.IllegalArgumentException();
!         }

!         firstDash = s.indexOf('-');
!         secondDash = s.indexOf('-', firstDash+1);
!         if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length()-1)) {
!             yyyy = s.substring(0, firstDash);
!             mm = s.substring(firstDash + 1, secondDash);
!             dd = s.substring(secondDash + 1);
!             if (yyyy.length() == YEAR_LENGTH && mm.length() == MONTH_LENGTH &&
!                     dd.length() == DAY_LENGTH) {
!                 year = Integer.parseInt(yyyy)- 1900;
!                 month = Integer.parseInt(mm) - 1;
!                 day = Integer.parseInt(dd);
!               parsedDate = true;
!             }
!         }
!
!         if (!parsedDate)
!             throw new java.lang.IllegalArgumentException();
!         else
!             return new Date(year, month, day);
      }
Diff from Lance.
                                     
2009-11-10
EVALUATION

Fix has been delivered to sustaining.  I will be checking it in to OpenJDK.

Please note that this has been an issue in the java.sql.Date code since JDBC 1.0 so this is *not* a regression, but a long outstanding bug.
                                     
2009-11-10



Hardware and Software, Engineered to Work Together