java JSF convertDateTime 呈现前一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12351244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF convertDateTime renders the previous day
提问by kostja
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
日期(作为日期时间类型)存储在我的数据库中,如下所示:31.10.2012
当我在 UI 中显示它时,默认情况下它是这样呈现的:2012-10-31
我使用它进行转换<f:convertDateTime pattern="dd.MM.yyyy" />
,意外地变成了30.10.2012
The SimpleDateconverter
, fed with the same date and the same format string returns 31.10.2012
, as expected.
的SimpleDateconverter
,供给具有相同的日期和相同的格式字符串返回31.10.2012
,如所预期。
What am I missing?
我错过了什么?
Thank you
谢谢
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.
编辑:对于存储为Timestamp
相同转换的日期会产生正确的结果,所以我认为它与Date
解释为确切的午夜有关,而午夜又可能被解释为属于 2 个不同的日子。但我仍然不知道在哪里定义行为以及最好的解决方法是什么。
回答by BalusC
This is undoubtedly a timezone-related issue.
这无疑是一个与时区相关的问题。
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.
JSF 在日期/时间转换中默认为 GMT (UTC)。因此,如果您的服务器平台默认时区是 GMT+X(而不是 GMT-X),那么时间将回溯到过去 X 小时。如果时间已经是00:00:00(午夜),那么日期甚至会回到过去一天。
There are 2 standard ways to achieve your functional requirement anyway:
无论如何,有两种标准方法可以满足您的功能要求:
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>
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" />
通过将以下上下文参数添加到 ,告诉 JSF 使用服务器平台默认时区代替所有日期/时间转换
web.xml
:<context-param> <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name> <param-value>true</param-value> </context-param>
更改 every
<f:convertDateTime>
以明确指定特定于 web 应用程序的时区。由于您位于德国并且日期格式模式也证实了这一点,我将假设 CET。<f:convertDateTime ... timeZone="CET" />
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.
在任何情况下,都不建议在整个应用程序中使用非通用或什至混合时区。建议将所有层和环境中的时区设置为 UTC。不仅在服务器和前端层和表示层,而且在 SQL 数据库和后端层和持久层。这样,代码对时区和 DST(!) 相关问题不敏感,如有必要,您可以只在演示期间专注于更改时区。