From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: aydin.toprak@intengo.com
To: rwa@mosaic-ag.com
Cc: pgsql-jdbc@postgresql.org
Subject: Re: How to insert "date" as timestamp
Date: Thu, 13 Oct 2005 09:23:23 -0500
Message-ID: <s34e27b0.054@gwmta.wicourts.gov> (raw)
Be careful about that with java.sql.Date.
To demonstrate the problem:
public class TestDate
{
public static void main(String[] args)
throws InterruptedException
{
java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());
java.sql.Date sqlDate1 = new java.sql.Date(utilDate.getTime());
Thread.sleep(20L);
utilDate = new java.util.Date(System.currentTimeMillis());
java.sql.Date sqlDate2 = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate1 + " equals " + sqlDate2 + " ? " + sqlDate1.equals(sqlDate2));
java.sql.Timestamp ts = new java.sql.Timestamp(sqlDate1.getTime());
System.out.println(ts);
ts = new java.sql.Timestamp(sqlDate2.getTime());
System.out.println(ts);
}
}
I get the following results:
2005-10-13 equals 2005-10-13 ? false
2005-10-13 09:17:40.431
2005-10-13 09:17:40.461
Unfortunately, the burden is on the application programmer
to provide a ms value which is at midnight for the default
time zone for the JVM when using that constructor. I can't
understand that as a design choice, but that's the current
reality.
-Kevin
>>> Roland Walter <rwa@mosaic-ag.com> 10/13/05 4:10 AM >>>
The conversion works as the following, i. e.:
java.util.Date date = new java.util.Date(System.currentTimeMillis());
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
It is the same for the conversion to java.sql.Date.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: Kevin.Grittner@wicourts.gov, aydin.toprak@intengo.com, rwa@mosaic-ag.com
Subject: Re: How to insert "date" as timestamp
In-Reply-To: <s34e27b0.054@gwmta.wicourts.gov>
References: <s34e27b0.054@gwmta.wicourts.gov>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox