为 f:convertDateTime 设置默认时区

编程入门 行业动态 更新时间:2024-10-10 12:26:23
本文介绍了为 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>

这样 JSF 将使用由 TimeZone#getDefault() 作为转换器的默认时区.

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 设置默认时区

本文发布于:2023-11-23 22:21:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   convertDateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!