Thread:
 store timestamps in UTC in DB, use Calendar from J... 
 GeraldLoeffler   21 Jul 2005, 10:15 
 Re: store timestamps in UTC in DB, use Calendar... 
 boomreddy   19 Mar 2007, 01:58 
 Re: store timestamps in UTC in DB, use Calen... 
 wilfred   21 Jan 2008, 05:14 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: GeraldLoeffler (21 Jul 2005, 10:15) Replies: 2, Views: 43430
Subject: store timestamps in UTC in DB, use Calendar from Java
hi!

In case anybody is interested: Here is my take on this very common
problem of handling timestamps expressed in arbitrary timezones:

I understand that there is no easy and portable way to store timestamps
with timezone information in the database. Thus I'm using SQL type
TIMESTAMP (which has no timezone information) to store timestamp
information and normalize all timestamps in the database to UTC.

A UTC database timestamp is mapped as a Hibernate timestamp type to a
java.util.Date. A java.util.Date also has no timezone information.

However, the getter/setter for a timestamp property on the Java entity
type returns/takes a java.util.Calendar object. A java.util.Calendar
contains timezone information, thus to the client of the Java class
there can never be any doubt in which timezone a timestamp is expressed.

(I use Hibernate field access for the Hibernate mapping because of the
above.)

The setter immediately converts the Calendar timestamp to a
java.util.Date in UTC to set the entity's field. Conversely, the getter
constructs a java.util.Calendar with timezone UTC and initializes it
from the entity's field.

Thus, the information about the timezone in which the client originally
expressed a timestamp is lost, but the timezone handling is otherwise
correct.

  cheers,
  gerald
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]