Search code examples
datejavabeansdate-formatspring-3spring-el

Can SpEL use a special date format for a bean property?


I have a bean with a date property called birthday:

<property name="birthday" value="06/12/1980"/>

Can I specify a date format for my bean, in order to get 6th December or 12th June or is the default one always used?


Solution

  • There are options:

    • Inject the date and format in your bean to return the desired string representation.
    • Use the Formatter SPI and configure an instance of DateFormatter given to you out of the box. Here is the Javadoc.
    • Implement and register your own instance of Formatter and then pass it to @Formatted annotation. Or use other annotations for more special cases. This is described here.