开发者

Set a default time zone for f:convertDateTime

I use JSF1.2 and I have a little problem with time zones.

Calendar respects my timezone and save the correct time in the Database. When I show it using a h:outputtext with a f:convertDateTime it shows the wrong date (I think with default time zone).

I can do 开发者_JS百科something like:

<h:outputText value="#{atividade.atividade.dataCriacao.time}">
  <f:convertDateTime pattern="#{msg.formatoDataCalendario2}" timeZone="America/Sao_Paulo" />
</h:outputText>

formatoDataCalendario2 = dd/MM/yyyy, HH:mm in messages.properties.

I can put the time zone in messages too, but I believe it have some configuration that I could use.


JSF date/time converters defaults by specification to UTC timezone. If you want to use a different timezone, then you really need to specify it in the converter yourself. Or, if you have 100% control over the production runtime environment, then since JSF 2.0 you can set its system timezone to the desired timezone and 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's timezone as obtained by TimeZone#getDefault() as converter's default timezone.

Please note that the java.util.Date object by itself also does not store any timezone information. It also always defaults to UTC timezone. Keep this in mind when processing submitted date/times.

See also:

  • Daylight saving time and time zone best practices


I had scenario like this. In my JSF app I was using as @BalusC pointed out:

<context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>

, and I had in xhtml:

<f:convertDateTime pattern="dd MMM yyyy - HH:mm:ss" type="date" />

However, when I receive date/time like 2020-02-18T10:15:20, That would be converted into GMT time (- 8 hours for me in PST zone). So, it would end up being 2020-02-18T02:15:20. That is because convertDateTime treats dates/times without time zone offset as GMT time by default so it converted it to GMT time.

Once I started receiving offset as well like 2020-02-18T10:15:20-07:00, my date/time would not longer be converted to GMT since it would recognize that the offset is matching the PST zone offset and would therefore do no conversion to GMT.

Hope that helps little bit.


For displaying date correctly you need to modify only in your web.xml

<context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>

and in your xhtml file

<h:outputText value="#{report.date}">
    <f:convertDateTime pattern="dd-MMM-yyyy" />
</h:outputText>

Its a timezone issue. The context param should fix it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜