I have a form with a field that uses a date/time picker which produces a hidden field with the full date & time in the ‘British’ format dd/MM/yy HH:mm through the use of javascript. This is submitted to a struts2 action whch has getters & setters for the field with operate on java.util.Date.
In the JSP:
<s:form>
...
<input type="hidden" name="myDate" value="08/05/11 16:00" />
<s:submit />
</s:form>
In the action
public class MyAction
{
public void setMyDate(Date d)
{
this.d = d;
}
public Date getMyDate()
{
return d;
}
}
As expected in Firefox when I submit the form struts automatically parses the date and I end up with a Date object in the action with the correct date.
However in Google Chrome when I submit the form the date is parsed as if it is in the US format MM/dd/yy HH:mm and as a result I get 5th August instead of 8th May.
Inspecting the HTTP headers sent by both browsers reveales that despite my OS language set to en_GB google chrome still sends the ‘Accept-Language’ header as en_US which means struts must be using this to determine what date format to use.
I can only assume this is a bug in Google Chrome however given that I don’t have the freedom to fix google chrome is there a way in Struts 2 that I can specify a fixed date format which tells Struts the format it should use to parse dates in form parameters?
In order to avoid misinterpretation of dates caused by different Locale values, I would go with an unambiguous and well-defined method of representing dates and times. This is what the ISO 8601 standard covers.
Unfortunately I don’t think Struts2 can convert to Date given this representation “out of the box”. For date conversion, by default, Struts2 uses the SHORT format for the Locale associated with the current request.
I guess you could try finding a plugin that already converts ISO 8601 dates or a simple approach would just be to write your own.