Cam
Cam

Reputation: 378

JSF 2 Default DateTime Converter Pattern

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.)

Upvotes: 4

Views: 6168

Answers (1)

BalusC
BalusC

Reputation: 1109715

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.

Upvotes: 6

Related Questions