JSF 2 Default DateTime Converter Pattern

converters, datetime-format, jsf

Solution

Just extend the `DateTimeConverter` class behind `<f:convertDateTime>` and set the defaults in the constructor.

@FacesConverter("defaultDateConverter")
public class DefaultDateConverter extends DateTimeConverter {

    public DefaultDateConverter() {
        setPattern("MM/dd/yyyy h:mm a");
    }

}

Use it as `<f:converter converterId="defaultDateConverter" />`

Please note that I omitted other attributes as they are ignored anyway when `pattern` is specified.

Problem

My JSF pages display DateTime from managed beans in this format: "MM/dd/yyyy h:mm a" I want avoid duplicate converter declaration in different pages: `<f:convertDateTime type="both" pattern="MM/dd/yyyy h:mm a" dateStyle="short" timeStyle="medium" />` Is there a way to make the above converter default for all DateTime fields? (Experience with JSF 2: 2 months.)

Original source