I am using the following JSF converter for my date inputs.
<f:convertDateTime dateStyle="medium" timeZone="EST" type="date" />
I want to centralize the converter so that I can change the timezone or the datestyle in one place. How is that possible?
Can I override the <f:convertDateTime>
?
The only way is relying on system default timezone (instead of UTC as internally used by JSF). So, if you have 100% control over the production runtime environment and the production system platform timezone is EST, then just add the following context parameter to web.xml
:
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
This way JSF will use the system platform timezone as obtained by TimeZone#getDefault()
as converter's default timezone instead of UTC.
If you have no control over it, then your best bet is to create an application scoped bean holding that property and reference it instead:
<f:convertDateTime ... timeZone="#{app.timeZone}" />
You can extend the DateTimeConverter
class behind <f:convertDateTime>
as follows in order to get a converter with all properties already set, but you would not be able to declare additional attributes from the view side on without wrapping it in a custom tag (which requires a TagHandler
and some XML boilerplate):
@FacesConverter("defaultDateConverter")
public class DefaultDateConverter extends DateTimeConverter {
public DefaultDateConverter() {
setDateStyle("medium");
setType("date");
setTimeZone(TimeZone.getTimeZone("EST"));
}
}
Use it as <f:converter converterId="defaultDateConverter" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With