TimeZone related issues in org.postgresql.jdbc2.TimestampUtils

From: "Carsten Klein" <carsten(dot)klein(at)axn-software(dot)de>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: TimeZone related issues in org.postgresql.jdbc2.TimestampUtils
Date: 2009-09-13 20:31:11
Message-ID: 6a89dba438112783b27f59f66336472f.squirrel@webmail.axn-software.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc


Hi All,

in an application that uses TIMESTAMP WITH TIME ZONE as the type for
certain attributes, we stumbled across the problem that, sometimes, the
values returned from the database will be adjusted so that the time zone
offset will be added/subtracted from the actual time stored in the
database.

Example: "2009-09-11 15:00:00+02" will be returned by TimestampUtils as
"2009-09-11 13:00:00".

This is due to the fact that in TimestampUtils.toTimestamp(...) and
TimestampUtils.toTime(...) the following will be used for returning the
Time or Timestamp instances, respectively:

toTimestamp(): Timestamp result = new Timestamp(useCal.getTime().getTime());

toTime(): Time result = new Time(useCal.getTime().getTime());

This however, leads to the aforementioned, incorrect results, since the
Calendar implementation, on getTime() will now adjust the initially
correct date and time based on the time zone information that is stored
with the data.

Whilst trying to work around that problem, I found out, that it would be
best to use

toTimestamp():
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone( useCal.getTimeZone() );
Timestamp result = new Timestamp( df.parse( df.format( useCal.getTime()
) ).getTime() );

toTime():
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone( useCal.getTimeZone() );
Time result = new Time( df.parse( df.format( useCal.getTime() )
).getTime() );

This then will lead to the correct results.

TIA for fixing this!

Best Regards

Carsten Klein

--

axn software UG (haftungsbeschränkt)
Wipperfürther Str. 278
51515 Kürten

HRB 66732
Gerichtsstand Amtsgericht Bergisch Gladbach

Telefon +492 268 801 285
Telefax +492 268 801 285
Mobil +491 577 666 256 5

WWW http://www.axn-software.de
Email info(at)axn-software(dot)de

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Oliver Jowett 2009-09-13 23:22:31 Re: TimeZone related issues in org.postgresql.jdbc2.TimestampUtils
Previous Message Thomas Kellerer 2009-09-07 07:37:25 Re: Need help to download jdbc driver in Dspace windows