Re: Timestamp vs. Java Date/Timestamp

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Timestamp vs. Java Date/Timestamp
Date: 2013-02-13 16:13:06
Message-ID: kfge0s$l8m$1@ger.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Andreas Reichel, 13.02.2013 06:37:
> On the same time java.sql.Date and java.util.Date support time
> information and in fact you can handover a java.sql.Date parameter to a
> Timestamp field.

But due to the removing of the time part this will *not* work.

> This is the part when I get confused: the timestamp field holds time
> information, the java.sql.Date parameter holds time information -- still
> it is cut off in the middle.

Yes, that's per documentation.

http://docs.oracle.com/javase/6/docs/api/java/sql/Date.html

"To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date
instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero [...]"

Note the part about "setting ... to zero"

So as soon as you create a java.sql.Date you lose the time information.

If you want to be JDBC compliant there are only two simple things to remember:

* use java.sql.Date together with setDate()/getDate() for date columns
* use java.sql.Timestamp together with setTimestamp()/getTimestamp() for timestamp columns

This works across all DBMS that I have worked with
(Oracle is a bit special but in general you should simply treat their DATE as timestamp)

The only problem are columns with timezones as none of the Java classes can deal with that.

Thomas

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message dmp 2013-02-13 17:26:25 Re: Timestamp vs. Java Date/Timestamp
Previous Message Bryan Varner 2013-02-13 15:20:41 Re: PostgreSQL XAResource & GlassFish 3.1.2.2