United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6763465 Issue with TimeStamp.valueOf method of JDK 1.6
6763465 : Issue with TimeStamp.valueOf method of JDK 1.6

Details
Type:
Enhancement
Submit Date:
2008-10-24
Status:
Closed
Updated Date:
2011-07-20
Project Name:
JDK
Resolved Date:
2011-07-20
Component:
core-libs
OS:
windows_2003
Sub-Component:
java.sql
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6u10
Fixed Versions:
7

Related Reports

Sub Tasks

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



Hardware and Software, Engineered to Work Together