f:convertDateTime 显示错误的日期 [英] f:convertDateTime displays wrong Date
问题描述
在我的 Web 应用程序中,我使用 Hibernate 检索数据并将其显示在 RichFaces 数据表中.
In my Web Application I retrieve data using Hibernate and display it in a RichFaces dataTable.
在我的 MySQL 表中有一个日期"类型的字段.当我将此字段打印到 Bean 中的日志时,它会显示数据库中的正确日期(例如 2010-04-21).但是在rich:dataTable中它显示如下:
In my MySQL-table there is a field of type "date". When I print this field to the log in my Bean, it shows the correct date from database (e.g. 2010-04-21). But in the rich:dataTable it shows up like this:
4/20/10
所以有 1 天的差异!
我添加了f:convertDateTime"转换器并将type"属性设置为both"以便也显示时间.所以现在它显示:
I added the "f:convertDateTime" converter and set the "type" attribute to "both" in order to display time too. So now it shows:
4/20/10 晚上 10:00:00
4/20/10 10:00:00 PM
我使用过的f:convertDateTime"代码:
The Code for "f:convertDateTime" I've used:
<f:convertDateTime locale="locale.US" type="both" dateStyle="short"/>
所以看起来 f:convertDateTime 好像在做梦,因为 MySQL-table 字段中没有时间信息!
So it seems like f:convertDateTime dreams up some time because there is no time information in the MySQL-table field!
我做错了什么?我需要做什么才能显示正确的日期?
谢谢汤姆
推荐答案
JSF 的日期/时间转换器默认为 UTC 时区.要覆盖它,您需要在每个日期/时间转换器中设置 timeZone
属性.这是一个使用 EDT 时区的示例(假设您位于美国东部).
JSF defaults to UTC timezone for date/time converters. To override this you need to set the timeZone
attribute in every date/time converter. Here's an example using EDT timezone (assuming you're on the east of US).
<f:convertDateTime locale="en_US" type="both" dateStyle="short" timeZone="EDT" />
locale
属性只控制完整的日/月名称格式(变成英文).
The locale
attribute only controls the full day/month name formatting (it becomes English).
如果要将默认UTC时区覆盖为操作平台默认时区,则需要在web.xml
中添加如下上下文参数:
If you want to override the default UTC timezone to be the operating platform default timezone, then you need to add the following context param 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 <f:convertXxx>
标签.
Then you don't need to edit every individual JSF <f:convertXxx>
tag.
这篇关于f:convertDateTime 显示错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!