JDK-6206215 : [Fmt-Da] Incorrect format date string is not checked
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.3.1_12
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-12-09
  • Updated: 2010-08-03
  • Resolved: 2005-08-17
Related Reports
Relates :  
Relates :  
Description
Dateformatter.parse does not seem to check the invalid date string.

REPRODUCE:
 1) compile the attached program (test_format.java)
 2) invoke the attached .bat file(exec_test_format.bat)

You will see the following messages 2st.
This is correct case.

c:\temp> java test_format "2004/12/07 11:15:30" 
Tue Dec 07 11:15:30 JST 2004
Tue Dec 07 11:15:30 JST 2004

Then you will see the results of invalid date string cases.

c:\temp> java test_format "2004/12/07 11:15:3b" 
Tue Dec 07 11:15:03 JST 2004
Tue Dec 07 11:15:03 JST 2004

c:\temp> java test_format "2004/12/07 11:15:3b1" 
Tue Dec 07 11:15:03 JST 2004
Tue Dec 07 11:15:03 JST 2004

Even if we set the invalid date string like "2004/12/07 11:15:3b",
The invalid portin of string("3b" and  "3b1") seems parsed as "03".

Such invalid string shoule be checked and VM should notify by an exception or 
message.

CONFIGURATION :
  - JRE : 1.3.1_12/1.4.2_06/5.0fcs/6.0b14
  - OS : WindowsXP(SP1, Japanese)
 



###@###.### 2004-12-09 06:05:34 GMT

Comments
EVALUATION This is not a defect. According to Java API Doc for SimpleDateFormat, "For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields." Therefore, even if "yyyy/MM/dd HH:mm:ss" is specified as a pattern, The given string doesn't need to have two-digit number for "ss". And, your code written in Workaround is not a workaround but a recommended code for such a case. If it doesn't work, please file a new bug.
17-08-2005

WORK AROUND I wonder if the following works as workaround. --- public Date parse(String text) throws ParseException { ParsePosition pp = new ParsePosition(0); Date date = super.parse(text, pp); if (pp.getIndex() != text.length()) { throw new ParseException("input text not match", pp.getIndex()); } return date; } --- ###@###.### 2004-12-09 06:05:34 GMT
09-12-2004