Reputation: 2444
I have a spring application which uses hibernate. The requirement is to persist and retrieve the timestamps in GMT. I have set the application timezone to GMT using context listener as below.
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
While the application now persists the timestamps in GMT consistently, while retrieving the timestamps from hibernate layer are inconsistent. The timestamps are returned some times in PST (the server's timezone).
My data retrieval query is as below
Query q=entityManager.createQuery("SELECT DISTINCT f FROM foo as f WHERE f.ID=?1 AND f.ID2=?2 ORDER >BY ID DESC").setParameter(1, id1).setParameter(2, id2);
q.setFirstResult(skip).setMaxResults(top).getResultList();
Any ideas on solving this issue?
Upvotes: 0
Views: 192
Reputation: 691755
A timestamp doesn't have any time zone. It's only when you display its (absolute) value that a time zone is used to give you a meaningful time representation.
Just use the appropriate time zone each time you want to display a time stamp (by setting the default time zone or by setting it on the date format used to display the timestamp).
Moreover, IIRC, setting the default time zone only sets it for the current thread and the threads started from the current thread. The other threads are unaffected.
Upvotes: 2