Re: Issue with the JDBC driver doing timezone conversions on "java.sql.Date"?

From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Fred Janon <fjanon(at)gmail(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Issue with the JDBC driver doing timezone conversions on "java.sql.Date"?
Date: 2009-12-21 08:08:13
Message-ID: 4B2F2CED.10400@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Fred Janon wrote:

> When I check the dates in the database using PGAdmin III on the client,
> I see the dates in the client timezone (GTM +8).
> If I do a
> TimeZone.setDefault(TimeZone.getTimeZone("GMT"))
> in my application, I see the dates with the "correct" (or expected) GMT
> timezone.
>
> I don't understand why there is any timezone involved in reading and
> writing "timestamp without time zone".

Because "timestamp without time zone" is a little counterintuitive. It
represents a particular wallclock date/time; it does not represent a
unique instant in time.

It's roughly like storing the string "2009-12-21 21:00:00" - that time
interpreted in my timezone is right now, but it'll be some different
instant when interpreted in your timezone.

So when the JDBC driver needs to convert between a Java Timestamp -
which *does* represent a particular instant in time, in terms of
milliseconds-since-the-epoch - and a timestamp-without-timezone, it has
to pick a particular timezone to do that. The driver follows the JDBC
spec and uses the JVM's default timezone unless you explictly provide a
Calendar to use when calling getTimestamp()/setTimestamp().

If you're trying to store particular instants-in-time, you probably want
to use TIMESTAMP WITH TIME ZONE (which, again a little
counterintuitively, does NOT actually store a timezone - it just stores
milliseconds-since-the-epoch and converts that to/from different
timezones on demand)

-O

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Alexander Pyhalov 2009-12-21 09:19:52 Re: Get bytes sent to client
Previous Message Craig Ringer 2009-12-21 07:52:30 Re: Cheapest way to poll for notifications? & Driver improvement question re SSL and notify