JDK-4920608 : Invalid parsing of invalid times in SimpleDateFormat
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.2_01
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-09-10
  • Updated: 2003-10-03
  • Resolved: 2003-10-03
Related Reports
Relates :  
Description

Name: gm110360			Date: 09/10/2003


FULL PRODUCT VERSION :


FULL OS VERSION :
Several versions of Windows (XP, 2000)

A DESCRIPTION OF THE PROBLEM :
Simple Date Format will parse incorrect times.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code shown below

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Errors when trying to parse invalid times.
ACTUAL -
See output

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 * @author Peter L Bird
 *
 * "OutOfTimeRange.java" created on Jun 18, 2003 at 4:14:57 PM
 */
public final class OutOfTimeRange
{
	public static void tryThisOne( String time )
	{
		try
		{
			System.out.println( " The Value of \""
									+ time
									+ "\" is\t"
									+ new Date( new SimpleDateFormat( "H:mm" ).parse( time ).getTime() ).toString()
							);
		}
		catch (Exception e)
		{
			System.err.println(" Er, no good " + e.getMessage());
		}
	}
	
	public static void main(String[] args)
	{
		tryThisOne( " 4:57" );
		tryThisOne( "23:57" );
		tryThisOne( "30:57" );
		tryThisOne( "99:57" );
		tryThisOne( "99:99" );
		tryThisOne( "999:99" );
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Check if the returned value is less than the number of milliseconds in a day.
(Incident Review ID: 188578) 
======================================================================

Comments
EVALUATION Use non-lenient mode for parsing, which will throw ParseException with invalid dates. See DateFormat.setLenient(boolean) documentation. The sample code works as intended in non-lenient mode. --- import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public final class OutOfTimeRange { public static void tryThisOne( String time ) { SimpleDateFormat sdf = new SimpleDateFormat("H:mm"); sdf.setLenient(false); try { System.out.println( " The Value of \"" + time + "\" is\t" + new Date(sdf.parse( time ).getTime()).toString() ); } catch (ParseException e) { System.err.println(" Er, no good " + e.getMessage()); } } public static void main(String[] args) { tryThisOne( " 4:57" ); tryThisOne( "23:57" ); tryThisOne( "30:57" ); tryThisOne( "99:57" ); tryThisOne( "99:99" ); tryThisOne( "999:99" ); } } --- Output: $ java -showversion OutOfTimeRange java version "1.4.2" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28) Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode) The Value of " 4:57" is Thu Jan 01 04:57:00 PST 1970 The Value of "23:57" is Thu Jan 01 23:57:00 PST 1970 Er, no good Unparseable date: "30:57" Er, no good Unparseable date: "99:57" Er, no good Unparseable date: "99:99" Er, no good Unparseable date: "999:99" ###@###.### 2003-10-03
03-10-2003