X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 0FE8332ABB2 for ; Thu, 14 Oct 2004 00:26:42 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 71050-06 for ; Wed, 13 Oct 2004 23:26:30 +0000 (GMT) Received: from cascavel.amazon.com.br (cascavel.amazon.com.br [200.241.240.57]) by svr1.postgresql.org (Postfix) with ESMTP id 0C13732AA47 for ; Thu, 14 Oct 2004 00:26:31 +0100 (BST) Received: from localhost (localhost [127.0.0.1]) by cascavel.amazon.com.br (Amazon-Mail) with ESMTP id B458D2F24F8 for ; Wed, 13 Oct 2004 20:25:26 -0300 (BRT) Received: from cascavel.amazon.com.br ([127.0.0.1]) by localhost (cascavel [127.0.0.1:10024]) (amavisd-new) with ESMTP id 11634-07 for ; Wed, 13 Oct 2004 20:25:25 -0300 (BRT) Received: from [127.0.0.1] (201009179114.user.veloxzone.com.br [201.9.179.114]) by cascavel.amazon.com.br (Amazon-Mail) with ESMTP id BF9932F1C51 for ; Wed, 13 Oct 2004 20:25:24 -0300 (BRT) Message-ID: <416DB991.3060809@amazon.com.br> Date: Wed, 13 Oct 2004 20:26:09 -0300 From: Jair da Silva Ferreira Jr User-Agent: Mozilla Thunderbird 0.7.3 (Windows/20040803) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: problem with dates when using a java calendar object with a non-default timezone Content-Type: multipart/mixed; boundary="------------090605010107090308020506" X-Virus-Scanned: by amavisd-new at Amazon Corporation X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/130 X-Sequence-Number: 11235 This is a multi-part message in MIME format. --------------090605010107090308020506 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, I am experiencing the following problem with postgresql: the date value I insert into the database is different from the value I select from the database. I use a Calendar with a non-default timezone to set and get the date value (using methods PreparedStatement.setDate(int,Date,Calendar) and ResultSet.getDate(int,Calendar)). Do you know how can I fix this problem? Am I doing anything wrong? Here is my system configuration: Server: OS: Red Hat Linux 7.3 Database: PostgreSQL 7.2 Client: OS: Windows XP SP2 Java: JDK 1.4.2_05 PostgreSQL JDBC: pg72jdbc2.jar and pg74.215.jdbc3.jar (the problem happened in both versions) I wrote a small Java program that shows the problem. This program is attached in this email. Above is the program output when executed in my client system. As you can see, "date (0) inserted" is different from "date (0) loaded", "date (2) inserted" is different from "date (2) loaded". This is exactly the problem. I don't care about the time (hour, minute and second) difference as this is a SQL DATE type. The problem is that the days in these dates are different. //program output start date (0) inserted: 13/10/04 00:00 date (1) inserted: 13/10/04 23:59 date (2) inserted: 13/10/04 08:00 date (3) inserted: 13/10/04 19:00 date (0) loaded: 12/10/04 17:00 date (1) loaded: 13/10/04 17:00 date (2) loaded: 12/10/04 17:00 date (3) loaded: 13/10/04 17:00 ********************************** ========= hour: 0 minute: 0 second: 0 millisecond: 0 ========= hour: 17 minute: 0 second: 0 millisecond: 0 ========= year1: 2004; month1: 9; date1: 13 year2: 2004; month2: 9; date2: 12 dates equals? false ********************************** ========= hour: 23 minute: 59 second: 59 millisecond: 999 ========= hour: 17 minute: 0 second: 0 millisecond: 0 ========= year1: 2004; month1: 9; date1: 13 year2: 2004; month2: 9; date2: 13 dates equals? true ********************************** ========= hour: 8 minute: 0 second: 0 millisecond: 0 ========= hour: 17 minute: 0 second: 0 millisecond: 0 ========= year1: 2004; month1: 9; date1: 13 year2: 2004; month2: 9; date2: 12 dates equals? false ********************************** ========= hour: 19 minute: 0 second: 0 millisecond: 0 ========= hour: 17 minute: 0 second: 0 millisecond: 0 ========= year1: 2004; month1: 9; date1: 13 year2: 2004; month2: 9; date2: 13 dates equals? true //program output end Any help would be much appreciated. Thanks, Jair Jr --------------090605010107090308020506 Content-Type: text/plain; name="JDBCTest2.java" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="JDBCTest2.java" import java.sql.*; import java.util.*; import java.text.*; public class JDBCTest2 { public static void execute(Connection c,List dates,GregorianCalendar cal,boolean useGregorianCalendarInJDBC)throws SQLException{ DateFormat df=new SimpleDateFormat(); df.setCalendar(cal); PreparedStatement ps=c.prepareStatement("insert into date_test values (?)"); int j=0; for(Iterator it=dates.iterator();it.hasNext();){ java.sql.Date d=(java.sql.Date)it.next(); if(useGregorianCalendarInJDBC){ ps.setDate(1,d,cal); } else{ ps.setDate(1,d); } ps.executeUpdate(); System.out.println("date ("+(j++)+") inserted: "+df.format(d)); } ps.close(); Statement s2=c.createStatement(); ResultSet rs=s2.executeQuery("select * from date_test "); List loadedDates=new ArrayList(); j=0; while(rs.next()){ java.sql.Date d; if(useGregorianCalendarInJDBC){ d=rs.getDate(1,cal); } else{ d=rs.getDate(1); } loadedDates.add(d); System.out.println("date ("+(j++)+") loaded: "+df.format(d)); } for(int i=0;i