Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

'timestamp with timezone' inconsistencies

Dan Scott GuestMar 17 2011 — edited Mar 21 2011
Hello,

I'm seeing an inconsistency between the output of SQLDeveloper and a JDBC Java application in a 'timestamp with timezone' column.

Here is my Java code:
package javaapplication2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        String url = "jdbc:oracle:thin:@dbhostname.com:1521:DB";
        conn = DriverManager.getConnection(url, "username", "password");
        Statement stmt = conn.createStatement();

        String query =
                "select to_timestamp_tz('2001-01-01 11:00:00.0 -5:00','YYYY-MM-DD HH24:MI:SSXFF TZH:TZM') from dual union all "
                + "select to_timestamp_tz('2001-06-01 11:00:00.0 -5:00','YYYY-MM-DD HH24:MI:SSXFF TZH:TZM') from dual union all "
                + "select to_timestamp_tz('2001-01-01 11:00:00.0 US/EASTERN','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all "
                + "select to_timestamp_tz('2001-06-01 11:00:00.0 US/EASTERN','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all "
                + "select to_timestamp_tz('2001-01-01 11:00:00.0 AMERICA/NEW_YORK','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all "
                + "select to_timestamp_tz('2001-06-01 11:00:00.0 AMERICA/NEW_YORK','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all "
                + "select to_timestamp_tz('2001-01-01 11:00:00.0 EST','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all "
                + "select to_timestamp_tz('2001-06-01 11:00:00.0 EST','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual  "
                ;

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            System.out.println(rs.getTimestamp(1));
        }
        rs.close();
        stmt.close();
        conn.close();
    }
}
and here are the results:
select to_timestamp_tz('2001-01-01 11:00:00.0 -5:00','YYYY-MM-DD HH24:MI:SSXFF TZH:TZM') from dual union all select to_timestamp_tz('2001-06-01 11:00:00.0 -5:00','YYYY-MM-DD HH24:MI:SSXFF TZH:TZM') from dual union all select to_timestamp_tz('2001-01-01 11:00:00.0 US/EASTERN','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all select to_timestamp_tz('2001-06-01 11:00:00.0 US/EASTERN','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all select to_timestamp_tz('2001-01-01 11:00:00.0 AMERICA/NEW_YORK','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all select to_timestamp_tz('2001-06-01 11:00:00.0 AMERICA/NEW_YORK','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all select to_timestamp_tz('2001-01-01 11:00:00.0 EST','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual union all select to_timestamp_tz('2001-06-01 11:00:00.0 EST','YYYY-MM-DD HH24:MI:SSXFF TZR') from dual  
2001-01-01 11:00:00.0
2001-06-01 12:00:00.0
2001-01-01 11:00:00.0
2001-06-01 11:00:00.0
2001-01-01 11:00:00.0
2001-06-01 11:00:00.0
2001-01-01 11:00:00.0
2001-06-01 12:00:00.0
When running the above query directly using SQLDeveloper (or sqlplus), I receive the following:
01-JAN-2001 11.00.00.000000000 -05:00
01-JUN-2001 11.00.00.000000000 -05:00
01-JAN-2001 11.00.00.000000000 US/EASTERN
01-JUN-2001 11.00.00.000000000 US/EASTERN
01-JAN-2001 11.00.00.000000000 AMERICA/NEW_YORK
01-JUN-2001 11.00.00.000000000 AMERICA/NEW_YORK
01-JAN-2001 11.00.00.000000000 EST
01-JUN-2001 11.00.00.000000000 EST
Can anyone help me explain the inconsistency between the two results. Ideally, I'd like to obtain the timezone returned by the database so that I can display it in the results. The java.sql.date object returned by rs.getTimestamp(1) does contain a depricated 'getTimeZoneOffset()' function. However this gives inconsistent results and I'd rather use the correct method. I'm using Oracle 11.2.0.2.0 and Java 1.6.0_24.

Thanks,

Dan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 18 2011
Added on Mar 17 2011
5 comments
1,335 views