Re: How to insert "date" as timestamp

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <aydin(dot)toprak(at)intengo(dot)com>, <rwa(at)mosaic-ag(dot)com>
Cc: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: How to insert "date" as timestamp
Date: 2005-10-13 14:23:23
Message-ID: s34e27b0.054@gwmta.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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(at)mosaic-ag(dot)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.

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kevin Grittner 2005-10-13 14:34:53 Re: How to insert "date" as timestamp
Previous Message Roland Walter 2005-10-13 14:20:55 Re: How to insert "date" as timestamp