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表中有一个类型为date的字段。当我将这个字段打印到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
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 PM
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中没有时间信息 - 表字段!
So it seems like f:convertDateTime dreams up some time because there is no time information in the MySQL-table field!
我做错了什么?我需要做什么才能显示正确的日期?
谢谢Tom
推荐答案
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时区作为操作平台的默认时区,那么您需要添加以下上下文param to 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屋!