Hello:
I am writing a custom converter for my dates in my app. The built-in converter populates text fields with a short date but I want MM/dd/yyyy since this is the format users enter in dates. The date fields are also optional in my app. That presents a problem.
Here is my customer conversion code:
public class ProjectDateConverter extends StrutsTypeConverter {
public Object convertFromString(Map map,
String[] strings, Class class1) {
String enteredDateString = strings[0];
if (!enteredDateString.trim().equals("")) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
try {
return sdf.parse(enteredDateString);
}
catch (ParseException e) {
throw new TypeConversionException("A date was not entered as MM/DD/YYYY");
}
}
System.out.println("Converter: " + enteredDateString);
return null;
}
public String convertToString(Map map, Object object) {
Date date = (Date) object;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
return sdf.format(date);
}
}
As you can see in the convertFromString method, some type of Object must be returned. If dates are optional, it would be ideal to return a null here but that, of course, doesn't work. How do I allow a user to optionally enter a date without the converter throwing an error? I have not been able to locate the built-in converter code yet.
Any help would be appreciated!
Eric