From: Andreas Reichel <andreas@manticore-projects.com>
To: Dave Cramer <pg@fastcrypt.com>
Cc: List <pgsql-jdbc@postgresql.org>
Subject: Re: Timestamp vs. Java Date/Timestamp
Date: Wed, 13 Feb 2013 05:37:25 +0000
Message-ID: <1360733845.24220.50.camel@localhost> (raw)
In-Reply-To: <CADK3HHJDvnHzcbEu2CMJ7g_riuMTi6D-=1mdvc+T-9DYEidvJA@mail.gmail.com>
References: <1360043227.14635.9.camel@localhost>
<CADK3HHJDvnHzcbEu2CMJ7g_riuMTi6D-=1mdvc+T-9DYEidvJA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
Dave,
please find a test attached. I guess, I can shed some light on it in the
meantime.
To me it seems, that setDate()/getDate() indeed ignores any time values
(hh:mm:ss.s) but parses the day-part only (yyyy-mm-dd).
At the same time, setTimestamp()/getTimestamp() behaves correctly.
(Note: in Postgres Date is Date only without Time, while Timestamp obeys
the Time, I was not completely aware of this. I thought the only
difference between Date and Timestamp is Milliseconds vs. Nanoseconds.)
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.
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.
Now I would like to suggest a couple of small changes:
a) allow java.util.Date in setObject() and convert it into
java.sql.Timestamp automatically
b) keep the time info whenever setting/returning java.sql.Date to/from a
Timestamp field
However, for the moment I am fine as I understood now the need for using
java.sql.Timestamp when working with time.
Thank you a lot and best regards
Andreas
PS:
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbcAttachments:Test.java (text/x-java, 2.8K) download
/*
*
* Copyright (C) 2012 Andreas Reichel <andreas@manticore-projects.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
package com.manticore.database;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public final static Logger logger=Logger.getAnonymousLogger();
public final static SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD hh:mm:ss.s");
public final static String timestampStr="2013-02-11 21:59:02.0";
public static java.sql.Timestamp getSqlTimestamp(String s) throws ParseException {
return new java.sql.Timestamp(sdf.parse(s).getTime());
}
public static java.sql.Date getSqlDate(String s) throws ParseException {
return new java.sql.Date(sdf.parse(s).getTime());
}
public static void main(String[] args) throws ClassNotFoundException, SQLException, ParseException {
String sqlStr="create table date_test (date_field timestamp)";
String sqlStr1="insert into date_test values(?)";
String sqlStr2="select date_field from date_test where date_field=?";
Class.forName("org.postgresql.Driver");
Connection conn=null;
PreparedStatement prep=null;
ResultSet rs=null;
try {
conn=DriverManager.getConnection("jdbc:postgresql:test", "test", "password");
try {
conn.createStatement().executeUpdate(sqlStr);
} catch (Exception ex) {
logger.log(Level.FINER, "Create Table", ex);
}
prep=conn.prepareStatement(sqlStr1);
prep.setObject(1, getSqlDate(timestampStr));
prep.executeUpdate();
prep=conn.prepareStatement(sqlStr2);
prep.setObject(1, getSqlDate(timestampStr));
rs=prep.executeQuery();
if (rs.next()) {
boolean valid = 0==rs.getDate(1).compareTo(sdf.parse(timestampStr));
logger.info("found date: " + rs.getDate(1) + " which is " + (valid ? " correct" : " NOT correct "));
}
} finally {
if (rs!=null) rs.close();
if (prep!=null) prep.close();
if (conn!=null) conn.close();
}
}
}
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: andreas@manticore-projects.com, pg@fastcrypt.com
Subject: Re: Timestamp vs. Java Date/Timestamp
In-Reply-To: <1360733845.24220.50.camel@localhost>
References: <1360043227.14635.9.camel@localhost> <CADK3HHJDvnHzcbEu2CMJ7g_riuMTi6D-=1mdvc+T-9DYEidvJA@mail.gmail.com> <1360733845.24220.50.camel@localhost>
* 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