JDK-6898593 : java.sql.Date.valueOf no exception if date given is not in the JDBC date escape format(yyyy-mm-dd)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.sql
  • Affected Version: 6u16
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-11-05
  • Updated: 2017-05-16
  • Resolved: 2010-04-08
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 Other Other JDK 6 JDK 7
1.4.2_26-revFixed 1.4.2_27Fixed 5.0u25Fixed 6u17-rev b09Fixed 7Fixed
Related Reports
Relates :  
Relates :  
Relates :  
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 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.
10-11-2009

EVALUATION See comments
10-11-2009

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.
10-11-2009