I couldn't find anything on this with Google, but I've got a 2ms offset
between the java.sql.Timestamp representation and the string
representation of a "timestamp with time zone".
I've tried the following JDBC releases: 8.1dev-401 JDBC 3, 8.0-312 JDBC
3, pg74.216.jdbc3.jar. The server is the Debian package of 7.4.7, though
I've seen the same problem against Postgres 7.2.
Here's a section from my JAVA code:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setCalendar(new GregorianCalendar(ServletBase.UTC));
PreparedStatement stmt = db.prepareStatement("insert into test values(1,?)"); //create a statement that we can use
later
Date date = format.parse("2005-05-12 17:14:21");
stmt.setTimestamp(1, new Timestamp(date.getTime()));
stmt.execute();
stmt = db.prepareStatement("select index, datetime from test");
Statement stmt2 = db.createStatement();
stmt2.executeUpdate("insert into test values(2, '2004-11-10 17:32:19')");
ResultSet rs = stmt.executeQuery();
DateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
output.setCalendar(new GregorianCalendar(ServletBase.UTC));
while(rs.next()) {
date = new Date(rs.getTimestamp(2).getTime());
System.out.println("Result " + rs.getInt(1) + " :- " + output.format(date));
}
The output from Java code is:
Result 1 :- 2005-05-12 17:14:21.000
Result 2 :- 2004-11-10 17:32:19.002
The database sees:
testcode=> select * from test;
index | datetime
-------+----------------------------
1 | 2005-05-12 17:14:20.998+00
2 | 2004-11-10 17:32:19+00
(2 rows)
--
Adrian Cox <adrian@humboldt.co.uk>