2

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!

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Great, yes that worked. But can you tell me, if the date is stored in the database as '01/05/2014' how does it know what timezone it was inputed in?
@DaveB An object of type java.util.Date itself has no timezone in its internal state. But the transfer from application layer via jdbc layer to database uses timezone conversion and can therefore change the stored milliseconds relative to UNIX epoch.
Thank you, yes that makes sense, also the linked question helped me out too
@MenoHochschild No, the input was not in UTC. The data type of the Postgres column was DATE. A Postgres DATE represents only a date (year, month, day-of-month) without a time portion, per the SQL-92 spec. The core problem here is retrieving date-only from the database, and then used to create a java.util.Date object which includes a time-of-day along with the date. A time portion is created in the process, presumably 00:00:00 for a time zone (UTC?).
@BasilBourque Yes, something like that. By the way, Postgre-SQL shows better standard-conform behaviour than Oracle.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.