Question

How do you bind a Date to in stripes using a specific format? "MM/dd/yyyy HH:mm:ss"

<s:text name="myDateTime" formatPattern="MM/dd/yyyy HH:mm:ss" />
Was it helpful?

Solution

Stripes uses Typeconverters for converting request parameters (Strings) to specific types. The javadoc for the default Date type converter can be found here:

net.sourceforge.stripes.validation.DateTypeConverter

To change the default formats it states:

This default set of formats can be changed by providing a different set of format strings in the Stripes resource bundle, or by sub classing and overriding getFormatStrings(). In all cases patterns should be specified using single spaces as separators instead of slashes, dashes or other characters.

And:

The keys used in the resource bundle to specify the format strings and the pre-process pattern are: stripes.dateTypeConverter.formatStrings and stripes.dateTypeConverter.preProcessPattern

If that does not do it for you, you can always roll you're own TypeConverter. This custom type converter can then be bound to a setter in the ActionBean by:

@Validate(converter=YourCustomTypeConverter.class)
public void setDate(Date date) {
  this.date = date;
}

If you want to let the mapping be done automatically you either need to override the default mapper or create another (sub) type. For example, you create your own type converter not for java.util.Date but for your own custom type that inherits from java.util.Date. As it's just a sub type without any extra behavior, the rest of the application can use it as java.util.Date.

Date date;

// No @validate needed, maps to MyCustomDate
public void setDate(MyCustomDate date) {
  this.date = date;
}     

OTHER TIPS

<fmt:formatDate timeZone="${timeZone}" value="${date}" pattern="MM/dd/yyyy hh:mm a"/>

SimpleDateFormat will be able to help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top