Pergunta

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

2014-05-01

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;
}

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>

It reads as

'30 Apr 2014'

So I remove the f:convertDateTime and it shows:

'5/1/2014'

So I then change the f:convertDateTime to:

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

And it reads:

'23:00 30 Apr 2014'

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!

Foi útil?

Solução

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>

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).

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

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top