JPA TemporalType.Date 给出错误的日期 [英] JPA TemporalType.Date giving wrong date

查看:33
本文介绍了JPA TemporalType.Date 给出错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有一个日期字段,表示一段数据的有效起始"日期.定义如下:

@Temporal( TemporalType.DATE )私人日期有效自;

在我从数据库中提取日期并显示它的那一刻,一切似乎都运行良好.如果我在前端选择日期 18-Sep-2003 然后在我检查数据库时保存它,这确实是保存的日期(数据库是 MySQL 5.5.9 列类型是 DATE).但是,当我调出列表记录时,显示的日期是 2003 年 9 月 17 日 - 前一天.

如果我选择年初或年末的日期,例如 2003 年 3 月 26 日或 2003 年 12 月 25 日,一切都很好,所以我猜这与夏令时有关,但错误在哪里?由于数据库似乎保存了正确的日期,我猜它必须是在 JPA 转换回 java.util.Date 时 - java.util.Date 是用于日期的最佳类吗?我见过一些人们使用 Calendar 的例子,但这似乎很重,我不确定它与基于 JSF 的前端一起工作的效果如何.

解决方案

经过多次试验和搜索,我很确定我已经找到了问题的原因.日期保存在带有所有时间和时区包袱的 java.util.Date 中.看起来 JPA 正在从数据库中读取日期 18 Sep 2003,然后像这样填充日期:Thu Sep 18 00:00:00 BST 2003"-注意时区已设置为 BST 可能是因为它不是由数据库显式设置.无论如何,如果你只想看到这样的日期,那么在JSF页面中格式化输出是必要的:

<f:convertDateTime pattern="dd MMM yyyy"/></h:outputText>

然而,这假设时区是机器上当前有效的时区.在我的例子中,时区当前是 GMT(因为现在是冬天),所以当显示日期Thu Sep 18 00:00:00 BST 2003"时,它会通过减去一小时将其转换为 GMT,使显示显示为 2003 年 9 月 17 日.

I have a class that has a date field representing a "valid from" date for a piece of data. It is defined like this:

@Temporal( TemporalType.DATE )
private Date validFrom;

All seems to be working fine right up the the point where I pull the date from the database and display it. If I select the date 18-Sep-2003 in the front end then save it when I check in the database sure enough that is the date held (database is MySQL 5.5.9 column type is DATE). However when I pull up a list records the date shown is 17 Sep 2003 - one day earlier.

If I choose a date early or late in the year like 26 Mar 2003 or 25 Dec 2003 everything is fine so I guessing this is something to do with daylight saving but where is the error creeping in? Since the database appears to be holding the correct date I'm guessing it must be when JPA is converting back into a java.util.Date - is java.util.Date the best class to use for a date? I've seen a few examples where people use Calendar but that seems pretty heavy weight and I'm not sure how well it will work with a JSF based front end.

解决方案

After much experimenting and searching I'm pretty sure I've found the cause of the problem. The date is held in a java.util.Date which comes with all the baggage of time and a timezone. It would seem that JPA is reading the date 18 Sep 2003 from the database and then populating the date like this: "Thu Sep 18 00:00:00 BST 2003" - notice the timezone has been set to BST probably because it wasn't explicitly set by the database. Anyway, it is necessary to format the output in the JSF page if you only want to see the date like this:

<h:outputText value="#{t.validFrom}">
    <f:convertDateTime pattern="dd MMM yyyy"/>
</h:outputText>

This, however, assumes that the timezone is whatever is currently in force on the machine. In my case the timezone is currently GMT (because it's winter) so when presented with the date "Thu Sep 18 00:00:00 BST 2003" it converts it to GMT by subtracting one hour leaving the display showing 17 Sep 2003.

这篇关于JPA TemporalType.Date 给出错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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