Postgresql日期类型和java SimpleDateFormat [英] Postgresql Date type and java SimpleDateFormat

查看:149
本文介绍了Postgresql日期类型和java SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型为date的数据库列,值为

I have a database column of type date with the following value

2014-05-01

然后我有以下hibernate映射:

I then have the following hibernate mapping:

@Temporal(TemporalType.DATE)
@Column(name = "end_date", nullable = false, length = 13)
public Date getEndDate() {
    return this.endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

在JSF页面中以下列方式引用此日期

When I reference this date in a JSF page in the following way

<h:outputText value="#{someBean.endDate}">
    <f:convertDateTime pattern="d MMM yyyy" />
</h:outputText>

它读作

'30 Apr 2014'

所以我删除f:convertDateTime,它显示:

So I remove the f:convertDateTime and it shows:

'5/1/2014'

所以我然后将f:convertDateTime更改为:

So I then change the f:convertDateTime to:

<h:outputText value="#{someBean.endDate}">
    <f:convertDateTime pattern="HH:mm d MMM yyyy" />
</h:outputText>

它显示为:

'23:00 30 Apr 2014'

所以我的猜测是,计算机设置为BST(英国夏令时间),数据库中的日期以某种方式设置为GMT(或祖鲁时间),以便-1小时导致日期在时间上移动到前一天?

So my guess is that my computer is set to BST (British Summer Time) and the date in the database is somehow set to GMT (or Zulu time) so that the -1 hour causes the date to move back in time to the previous day?

有没有办法阻止这种情况发生,我只是想把它当作一个日期,没有时间分量!

Is there any way to stop this happening, I just want to treat it as a date, with no time component!

推荐答案

您也应该指定时区(以及安全的英文语言环境):

You should specify the timezone, too (and also the english locale for safety):

<h:outputText value="#{someBean.endDate}">
    <f:convertDateTime pattern="d MMM yyyy" timeZone="GMT" locale="en" />
</h:outputText>

此处我假设输入是UTC-timezone(即文字日期最初存储在相对到UTC),所以对输出格式使用相同的偏移量将不会更改日期(5/1/2014的中间输出只是默认的US格式由相同的偏移效果改变)。

Hereby I assume that the input is in UTC-timezone (that is the literal date was originally stored relative to UTC), so using the same offset for output format will not change the date (and your intermedium output of "5/1/2014" was just the default US-format changed by the same offset effect).

如果您使用另一个时区转换存储了您的日期,那么您必须调整timeZone属性(例如Europe / London)。

If you have stored your date using another timezone conversion then you have to adjust the timeZone-attribute (for example "Europe/London").

更新:我发现这个 SO问题这也应该帮助你。

UPDATE: I have found this SO-question which should help you, too.

这篇关于Postgresql日期类型和java SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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