JSF convertDateTime渲染前一天 [英] JSF convertDateTime renders the previous day

查看:154
本文介绍了JSF convertDateTime渲染前一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日期(作为日期时间类型)存储在我的数据库中,如下所示: 31.10.2012

当我在UI中显示时,它默认为这样: 2012-10-31

我使用< f:convertDateTime模式转换它=dd.MM.yyyy/> 而且意外地变成了

30.10.2012

A date (as the Date temporal type) is stored in my DB like this: 31.10.2012
When I display it in the UI, it is rendered like this per default: 2012-10-31
I convert it using <f:convertDateTime pattern="dd.MM.yyyy" /> and unexpectedly it turns to
30.10.2012

SimpleDateconverter ,使用相同的日期和相同的格式字符串返回 31.10.2012 ,如预期的那样。

The SimpleDateconverter, fed with the same date and the same format string returns 31.10.2012, as expected.

我缺少什么?

谢谢

编辑:存储为 Timestamp 的日期相同的转换产生正确的结果,所以我想它与 Date 解释为确切的午夜,而这又可能被解释为属于2个不同的日子。但我仍然没有线索定义行为,什么是最好的解决方法。

EDIT : for dates stored as Timestamp the same conversion yields correct results, so I suppose it has something to do with the Date interpreted as the exact midnight which in turn might be interpreted to belong to 2 different days. But I still have no clue where to define the behaviour and what would be the best workaround.

推荐答案

这无疑是时区相关的问题。

This is undoubtedly a timezone-related issue.

JSF在日期/时间转换中默认为GMT(UTC)。所以如果你的服务器平台的默认时区是GMT + X(而不是GMT-X),那么时间会在过去的X时间里返回。如果时间已经是00:00:00(午夜),那么日期甚至会过去一天。

JSF defaults to GMT (UTC) in date/time conversion. So if your server platform default timezone is GMT+X (not GMT-X), then the time will go back in the past X-amount of hours. If the time is already 00:00:00 (midnight), then the date will even go back one day in the past.

有两种标准的方法来实现您的功能需求:

There are 2 standard ways to achieve your functional requirement anyway:


  1. 通过将以下上下文参数添加到 web.xml

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


  • 更改每个< f:convertDateTime> 显式指定特定于webapp的时区。当你在德国和日期格式模式也证实了这一点,我会假设CET。

  • Alter every <f:convertDateTime> to explicitly specify the webapp-specific timezone. As you're based in Germany and the date format pattern also confirms this, I'll assume CET.

    <f:convertDateTime ... timeZone="CET" />
    


  • 无论如何,使用非通用甚至整个应用程序的混合时区也是不推荐的。建议将所有图层和环境中的时区设置为UTC。不仅在服务器和前端层和表示层,而且在SQL数据库和后端层和持久层中。这样,代码对时区和DST(!)相关的事情并不敏感,如果需要,您只能专注于在演示期间更改时区。

    In any case, using a non-universal or even mixed timezone throughout the application is not recommendable. It's recommend to set the timezone in all layers and environments to UTC. Not only in the server and front end tier and presentation layer, but also in the SQL database and back end tier and persistence layer. This way the code is not sensitive to timezone and DST(!) related matters and you can just focus on altering the timezone during presentation only, if necessary.

    • Daylight saving time and time zone best practices

    这篇关于JSF convertDateTime渲染前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆