JSF convertDateTime 呈现前一天 [英] JSF convertDateTime renders the previous day

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

问题描述

日期(作为日期时间类型)存储在我的数据库中,如下所示:31.10.2012
当我在 UI 中显示它时,它默认呈现如下:2012-10-31
我使用 <f:convertDateTime pattern="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.

我错过了什么?

谢谢

EDIT :对于存储为 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 添加以下上下文参数,告诉 JSF 使用服务器平台默认时区代替所有日期/时间转换:

  1. Tell JSF to use the server platform default timezone instead for all date/time conversion by adding 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>

  • 更改每个 以明确指定特定于 Web 应用程序的时区.由于您位于德国并且日期格式模式也证实了这一点,因此我假设您使用的是 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.

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

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