Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date validation problem

Status
Not open for further replies.

4345487

Programmer
Dec 31, 2002
132
US

I expected an error from the code below but it does no go to the catch section any ideas

Thanks

SimpleDateFormat inputVal = new SimpleDateFormat("MM/dd/yyyy");

try
{
Date date = inputVal.parse("99/99/2006");
}
catch (ParseException e1)
{
System.out.println("Invalid date..");
}
 
Use the setLenient(false) call :

Code:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date date = sdf.parse("99/99/2006");

This results in an exception :
Code:
java.text.ParseException: Unparseable date: "99/99/2006"

This is all in the API documentation for the parse() method - always read the API first !

API said:
By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 


I want to thank everyone for your time and help

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top