this is a well known question already appeared on stackoverflow.
I have been asked to solve a bug in a web application.
I have a date coming from a web form, the user is supposed to enter a valid date in a given format dd/MM/yyyy
.
The application is using Struts 1.3 as a framework, and this is what I found in validate method of the corresponding FormBean.
DateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy", aLocale );
try{
mydate = dateFormat.parse( formDate );
}
catch( ParseException pe ){
errors.add( "error", new ActionError( "error.date.invalid" ) );
}
However it's happening that the error is not raised for example when user enter year in short format, i.e. 01/10/12
, which is converted into a date, looking in database I found 01/10/0012
.
As a quick fix I tried to use setLenient(false)
, but the exception is still not raised, and mydate still results in a valid but wrong date.
Before going to mess with regex and string pre parsing, I was wondering if I am missing something. Especially when locale is used within date format instantiation.
Current Jdk used in project is 1.5