Abhinav SInghvi
Abhinav SInghvi

Reputation: 411

How come struts property tag converts java.util.Date in m/d/yy format

I have a Date object in my action class, when I access that object in jsp using <s:property> tag I get Date in m/d/yy format without my intervention, why does that happen?

<s:property value="#xyz"/> where xyz is of type java.util.date show date in m/d/yy format

Upvotes: 2

Views: 1863

Answers (2)

nmc
nmc

Reputation: 8686

Struts recognizes certain object types as listed in the Built in Type Conversion Support section for the documentation and as explained in the linked answer by Andrea.

If you're wondering why your Date object render as m/d/yy format even though you are using an <s:property> tag instead of an <s:date> tag, it's because Date is one of the types that Struts2 has built-in conversion for.

So instead of calling the default toString() method of the Date object, Struts2 will use the built-in type converters to display the String.

In the com.opensymphony.xwork2.conversion.impl.StringConverter class, Struts2 will attempt to convert the Date object to a String in a meaningful way using the default format for the Locale as mentioned by Andrea and the documentation.

Upvotes: 3

Andrea Ligios
Andrea Ligios

Reputation: 50203

It is not changing anything: a date is a point in time. The format used to create it (nor the one chosen to display it) has nothing to do with its value.

Since you are not specifying any format, the framework is falling back to the default (DateFormat.SHORT) for the Locale (that probably is en_US) sent by the browser in an HTTP header (if not manually set by you).

You can read here some considerations on how the Date is handled by Struts2.

Note that the DateFormat.MEDIUM format is instead the default when using Struts2 <s:date> tag without specifying a format.

That said, you can set the default date format at application level, or inline in the <s:date /> tag.

Upvotes: 2

Related Questions