Using struts2 and ognl, dates are automatically converted from the input to a Date object in your controller (util.Date or sql.Date).
The conversion is done using the the SHORT format for the Locale associated with the current request.
If you want to change this, the fastest way is to override ognl default conversion, using a custom StrutsTypeConverter (here called UtilDateConverter and convertToString.
Note this isn't the best practice :
from Struts2 doc:
{{Type conversion should not be used as a substitute for i18n. It is not recommended to use this feature to print out properly formatted dates. Rather, you should use the i18n features of Struts 2 (and consult the JavaDocs for JDK's MessageFormat object) to see how a properly formatted date should be displayed.
}}
Here is the class for all your util.Date objects.
Just create the class in a com.converter package
package com.converter ; public class UtilDateConverter extends StrutsTypeConverter { public Object convertFromString(Map context, String[] values, Class toClass) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); try { Date date = (Date) sdf.parse(values[0]); return new java.sql.Date(date.getTime()) ; } catch (ParseException e) { return values[0]; } } public String convertToString(Map context, Object o) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); return sdf.format(o); } }
Same thing for sql.Date
package com.converter ; public class SqlDateConverter extends UtilDateConverter { public Object convertFromString(Map context, String[] values, Class toClass) { Date date = (Date) super.convertFromString(context, values, toClass); return new java.sql.Date(date.getTime()) ; } }
Now if we want this to be used by all our controller, we need to create the following "xwork-conversion.properties" at the root of the classpath :
java.util.Date=com.converter.UtilDateConverter java.sql.Date=com.converter.SqlDateConverter
That's it.
Conversions are now done using our own class :)
(Note that you can do the same but just for a specific controller)