Java f:convertDateTime 显示错误的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2689245/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:51:49  来源:igfitidea点击:

f:convertDateTime displays wrong Date

javahibernatejsfrichfacesjsf-2

提问by Tom

In my Web Application I retrieve data using Hibernate and display it in a RichFaces dataTable.

在我的 Web 应用程序中,我使用 Hibernate 检索数据并将其显示在 RichFaces 数据表中。

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:

在我的 MySQL 表中有一个“日期”类型的字段。当我将此字段打印到 Bean 中的日志时,它会显示数据库中的正确日期(例如 2010-04-21)。但是在 Rich:dataTable 中,它显示如下:

4/20/10

10 年 4 月 20 日

So there is a discrepancy of 1 day!

所以有1天的差异!

I added the "f:convertDateTime" converter and set the "type" attribute to "both" in order to display time too. So now it shows:

我添加了“f:convertDateTime”转换器并将“type”属性设置为“both”以显示时间。所以现在它显示:

4/20/10 10:00:00 PM

4/20/10 晚上 10:00:00

The Code for "f:convertDateTime" I've used:

我使用的“f:convertDateTime”代码:

<f:convertDateTime locale="locale.US" type="both" dateStyle="short"/>

So it seems like f:convertDateTime dreams up some time because there is no time information in the MySQL-table field!

所以看起来 f:convertDateTime 梦想了一些时间,因为 MySQL-table 字段中没有时间信息!

What am I doing wrong? What do I need to do to display the correct date?

我究竟做错了什么?我需要做什么才能显示正确的日期?

Thanks Tom

谢谢汤姆

回答by Bozho

You are probably having issues with the Time zones.

您可能遇到时区问题。

Check this articleon how to resolve them.

查看这篇文章,了解如何解决它们。

Before that, you can try explicitly setting the timeZoneattribute of <f:convertDataTime>

在此之前,你可以尝试明确设置timeZone的属性<f:convertDataTime>

回答by silb

We had the same problem and ended up with writing a new JSF converter that uses a SimpleDateFormat created with new SimpleDateFormat(String pattern)for the date conversion.

我们遇到了同样的问题,最终编写了一个新的 JSF 转换器,该转换器使用new SimpleDateFormat(String pattern)为日期转换创建的 SimpleDateFormat 。

回答by Zeemee

According to the JSF specs, f:convertDateTime defaults to UTC timezone (regardless of any VM timezone setting), which differs from your timezone by -1 hour (standard time) or -2 hours (summer time).

根据 JSF 规范,f:convertDateTime 默认为 UTC 时区(无论 VM 时区设置如何),它与您的时区相差 -1 小时(标准时间)或 -2 小时(夏令时)。

We use a application scoped page bean with an timeZone property like this:

我们使用具有 timeZone 属性的应用程序范围页面 bean,如下所示:

public TimeZone getTimeZone() {
    return TimeZone.getDefault();
}

Then we use the property in an EL expression:

然后我们在 EL 表达式中使用该属性:

<ice:outputText value="#{deliveryDate}">
    <f:convertDateTime type="both" timeZone="#{Application.timeZone}" />
</ice:outputText>

The advantage is that it's considering standard/summer time automatically.

优点是它会自动考虑标准/夏令时。

回答by BalusC

JSF defaults to UTC timezone for date/time converters. To override this you need to set the timeZoneattribute in every date/time converter. Here's an example using EDT timezone (assuming you're on the east of US).

JSF 默认为日期/时间转换器的 UTC 时区。要覆盖它,您需要timeZone在每个日期/时间转换器中设置属性。这是使用 EDT 时区的示例(假设您在美国东部)。

<f:convertDateTime locale="en_US" type="both" dateStyle="short" timeZone="EDT" />

The localeattribute only controls the full day/month name formatting (it becomes English).

locale属性仅控制完整的日/月名称格式(变为英文)。

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:

如果要将默认UTC时区覆盖为操作平台默认时区,则需要将以下上下文参数添加到web.xml

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

Then you don't need to edit every individual JSF <f:convertXxx>tag.

然后您不需要编辑每个单独的 JSF<f:convertXxx>标记。

回答by Tomasz

<h:outputText id="dateId" value="#{item.date}">
 <f:convertDateTime for="dateId" type="date" dateStyle="long" locale="pl"/>   </h:outputText>