import java.sql.*; public class zone { public static void main(String args[]) throws Exception { Class.forName("org.postgresql.Driver"); // Setup the Java client to be in PST8PDT. java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/Los_Angeles")); runWith(2); runWith(3); } private static void runWith(int protocolVersion) throws SQLException { System.out.println("Running with protocol: " + protocolVersion); Connection conn = DriverManager.getConnection("jdbc:postgresql://192.168.3.3:5432/jurka?protocolVersion="+protocolVersion,"jurka",""); Statement stmt = conn.createStatement(); // Setup the db server to be in EST5EDT. stmt.execute("SET TIMEZONE TO 'EST5EDT'"); // Create a test table showing the difference with/without tz. stmt.execute("CREATE TEMP TABLE tstest(a timestamp without time zone, b timestamp with time zone)"); // This timestamp object will have a timezone of the default // java setting we configured earlier. Timestamp ts = new Timestamp((new java.util.Date()).getTime()); System.out.println("Before insert = " + formatTS(ts)); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO tstest(a,b) VALUES(?,?)"); pstmt.setTimestamp(1, ts); pstmt.setTimestamp(2, ts); pstmt.executeUpdate(); pstmt.close(); ResultSet rs = stmt.executeQuery("SELECT a,b FROM tstest"); while (rs.next()) { System.out.println("getString() = getTimestamp()"); System.out.println(rs.getString(1) + " = " + formatTS(rs.getTimestamp(1))); System.out.println(rs.getString(2) + " = " + formatTS(rs.getTimestamp(2))); } rs.close(); stmt.close(); conn.close(); System.out.println(); } private static String formatTS(Timestamp ts) { int offset = ts.getTimezoneOffset(); int absoff = Math.abs(offset); int hrs = absoff/60; int mins = absoff - hrs*60; String sign = (offset > 0) ? "-" : "+"; String hrStr = ((hrs < 10) ? "0" : "") + hrs; String minStr = ((mins < 10) ? "0" : "") + mins; return ts.toString() + sign + hrStr + ":" + minStr; } }