2

Setup

  • Web application runs on machine A with system time set to GMT
  • Oracle database runs on machine B with system time set to GMT. (select dbtimezone from dual shows +0:00)

Problem

Web application uses Hades JPA and Hibernate to write and read from the DB. A table has two columns of type timestamp called CREATED and MODIFIED. The web applications uses joda DateTime to get java.util.Date object and writes it into the DB. Things are fine the first time the entry is created, both fields have time set in GMT. But once this same row gets updated the MODIFIED column gets set in PST.

It is the same code section that gets executed to set date values during creation as well as during modification (It tries getting java.util.Date from joda DateTime as mentioned above).

Can someone explain this behavior?

One more thing I have noticed is in the above setup if in the web application I set the date column using new Date(), it sets it in PST. I am clueless as where is it getting the timezone as PST when the application and DB are both running in machines set to GMT.

EDIT: TimeZone.getDefault() prints GMT in both the machines.

0

1 Answer 1

2

Check what timezone the JVM thinks it's running in by calling java.util.TimeZone.getDefault(). That would be the timezone it's applying to the Date when it's read from your jdbc driver. Although I've used hibernate, I'm not sure what configurability it has on the timezone. Nevertheless, I'd say under the hood it's calling java.sql.ResultSet.getDate(). This function has an overload getDate(String columnLabel, Calendar cal) which allows you to specify the timezone of the column it's reading from the database. That's probably the root cause of the problem. If Hibernate is calling the default though getDate(String columnLabel) then more than likely the problem is the timezone your JVM thinks it's in is not GMT.

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

2 Comments

Yea forgot to mention this TimeZone.getDefault() prints GMT in both the machines.
And if you toString the date when you read it back out of the database does the timezone say GMT?

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.