Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5ehn-000883-3G for pgsql-jdbc@arkaria.postgresql.org; Wed, 13 Feb 2013 15:57:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U5ehm-0001AD-HZ for pgsql-jdbc@arkaria.postgresql.org; Wed, 13 Feb 2013 15:57:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5V2L-0004Bi-94 for pgsql-jdbc@postgresql.org; Wed, 13 Feb 2013 05:37:41 +0000 Received: from moutng.kundenserver.de ([212.227.126.186]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5V2E-0004SI-Mq for pgsql-jdbc@postgresql.org; Wed, 13 Feb 2013 05:37:40 +0000 Received: from [192.168.0.10] (node-djg.pool-1-4.dynamic.totbb.net [1.4.196.140]) by mrelayeu.kundenserver.de (node=mrbap4) with ESMTP (Nemesis) id 0MK1R7-1U6Jyx2y4j-001Au7; Wed, 13 Feb 2013 06:37:32 +0100 Message-ID: <1360733845.24220.50.camel@localhost> Subject: Re: Timestamp vs. Java Date/Timestamp From: Andreas Reichel To: Dave Cramer Cc: List Date: Wed, 13 Feb 2013 05:37:25 +0000 In-Reply-To: References: <1360043227.14635.9.camel@localhost> Content-Type: multipart/mixed; boundary="=-HC8SK+ioOANDefXigqyc" X-Mailer: Evolution 3.6.3 Mime-Version: 1.0 X-Provags-ID: V02:K0:NnabtM2Hze7dwaGEituMin+DGHeU6ERgwff6KJb4PEt jWfZWJ69K6VLl/ABoMCpr+GWkxXLZ1JuCGtrLQfTGtP5LnICzw nFZJILvxVU+LavApu42b/OurgrxKIDtON0Ak0g7XdKeAjWso7Y i6GI6STc1cNib5ornkAbf6PYlNnoP+s9Wu2mefe3tZ6Efwytx4 KMY/iczNLZuryprSClCEjeAResb8XO/1xEjrs4cQ310hfldmtq rlThoYPHlt+1EHPlmsvu2TYn4lzZx4ByHfwdn+JIBwSSaHrIV/ ddWsoAOohoYRNaNEpdoJqTp9iJSSGTLQmth0wPJNIG/91imnMy F2kzY00Am/sbi9U5Po8uYtXnBaRsz6UFhTapwDbUd X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org --=-HC8SK+ioOANDefXigqyc Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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: --=-HC8SK+ioOANDefXigqyc Content-Disposition: attachment; filename="Test.java" Content-Type: text/x-java; name="Test.java"; charset="us-ascii" Content-Transfer-Encoding: 7bit /* * * Copyright (C) 2012 Andreas Reichel * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 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(); } } } --=-HC8SK+ioOANDefXigqyc Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc --=-HC8SK+ioOANDefXigqyc--