为f:convertDateTime设置默认时区 [英] Set a default time zone for f:convertDateTime
问题描述
我使用JSF1.2,时区存在一些问题.
I use JSF1.2 and I have a little problem with time zones.
日历尊重我的时区,并在数据库中保存正确的时间.当我使用带有f:convertDateTime的h:outputtext显示它时,它显示了错误的日期(我认为使用默认时区).
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).
我可以做类似的事情:
<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.
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日期/时间转换器根据规范默认为UTC时区.如果要使用其他时区,则确实需要在转换器中自行指定.或者,如果您对生产运行时环境有100%的控制权,那么从JSF 2.0开始,您可以将其系统时区设置为所需的时区,并将以下上下文参数添加到web.xml
:
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.
请注意,java.util.Date
对象本身也不会不存储任何时区信息.它还始终默认为UTC时区.处理提交的日期/时间时请记住这一点.
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.
这篇关于为f:convertDateTime设置默认时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!