JDK-6763465 : Issue with TimeStamp.valueOf method of JDK 1.6
  • Type: Enhancement
  • Status: Closed
  • Resolution: Fixed
  • Component: core-libs
  • Sub-Component: java.sql
  • Priority: P4
  • Affected Version: 6u10
  • OS: windows_2003
  • CPU: x86
  • Submit Date: 2008-10-24
  • Updated Date: 2014-09-30
  • Resolved Date: 2011-07-20
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 Availabitlity Release.

To download the current JDK release, click here.
JDK 7
7 b120Fixed
Description
FULL PRODUCT VERSION :
jdk1.6.0_10

ADDITIONAL OS VERSION INFORMATION :
Windows 2003 server

A DESCRIPTION OF THE PROBLEM :
It seems there is an issue with java.sql.TimeStamp class, valueOf() method.
When we execute: Timestamp.valueOf("2008-1-26 12:12:12")

	Sample code:
	public class TimestampTest {
		public static void main(String[] args) {
			Timestamp t = Timestamp.valueOf("2005-1-01 10:20:50.00");
		}
	}

Following exception is received:
Exception in thread "main" java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
	at java.sql.Timestamp.valueOf(Timestamp.java:194)

The Java version I am using is: jdk1.6.0_10.
The above line of code works fine with jdk 1.5 and former versions.

This happens only when we pass single digit in Date/Month parameter. (As in above example "1" is passed as month parameter instead of "01")

In Java 6, in the TimeStamp.java class, there is extra check done for the length of the year, month and day fields and restricting the year to 4, month to 2 and day to 2 characters.

Code snippet from the Timestamp.valueOf method (Java 6)
	int intDate[] = {4,2,2};
	if(tokenDate.length() != intDate[counterD] ) {

Has the above restriction deliberately imposed? This restriction was not there in previous releases. This will result in changes for the code which was relying on the above API.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Use following line of code to reproduce the issue with jdk 1.6

	public class TimestampTest {
		public static void main(String[] args) {
			Timestamp t = Timestamp.valueOf("2005-1-01 10:20:50.00");
                        System.out.println(t);
		}
	}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
2008-01-26 12:12:12.0
ACTUAL -
Exception in thread "main" java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
	at java.sql.Timestamp.valueOf(Timestamp.java:194)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
	public class TimestampTest {
		public static void main(String[] args) {
			Timestamp t = Timestamp.valueOf("2005-1-01 10:20:50.00");
                        System.out.println(t);
		}
	}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
use "01" instead of  "1" in the above example.

Release Regression From : 5.0u12
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION JDBC 4.1 allows for the leading 0 to be optional.
2010-09-17

EVALUATION The change in behavior is intentional and was done as part of the fix for 5006540. The javadocs are clear that the format is yyyy-mm-dd hh:mm:ss[.f...] There is nothing in the javadocs which indicates a leading 0 is optional. Now that being said, I do not have an issue relaxing the format for mm and dd so the leading 0 can be omitted, but not until Java SE 7 and JDBC 4.1 when I can update the javadocs accordingly. I have changed this to an RFE to the spec.
2008-10-24