desilijic
desilijic

Reputation: 104

Why have I diffrent timezone offset result in Oracle DB and in Java?

I have the following code to compare timezone offset of 'Asia/Novosibirsk' timezone in Oracle Database 11g (11.2.0.4.0) and in Java:

import oracle.jdbc.pool.OracleDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;


public class TZTest {
    public static void main(String[] args) {

        try {

            OracleDataSource oracleDataSource = new OracleDataSource();
            oracleDataSource.setURL("jdbc:oracle:thin:@some_host/some_sid");
            oracleDataSource.setUser("some_user");
            oracleDataSource.setPassword("some_passowrd");

            try (Connection connection = oracleDataSource.getConnection();
                 Statement statement = connection.createStatement();
                 ResultSet resultSet = statement.executeQuery(
                         "SELECT TZ_OFFSET(sessiontimezone) tzo, sessiontimezone tzn FROM dual")
            ) {
                while (resultSet.next()) {
                    System.out.println(
                            String.format("From DB:   %s %s",
                                    resultSet.getString("tzn"),
                                    resultSet.getString("tzo"))
                    );
                }
            }

            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime now = LocalDateTime.now();
            ZonedDateTime zonedDateTime = now.atZone(zoneId);
            ZoneOffset offset = zonedDateTime.getOffset();

            String out = String.format("From Java: %s %s", zoneId, offset);
            System.out.println(out);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}  

And I get that result :

From DB:   Asia/Novosibirsk +07:00 
From Java: Asia/Novosibirsk +06:00

Why the offsets of this timezone is different ?

I use this driver and this java version

ojdbc6.jar  (version 11.2.0.3.0 )
java.vendor: Oracle Corporation   
java.version: 1.8.0_40           
JRE tzdata version: tzdata2015a   

Upvotes: 1

Views: 584

Answers (2)

Kalyan Chavali
Kalyan Chavali

Reputation: 1348

I am not entirely sure, but might be the server which hosts your database and the one in which your java code runs are in different data centers which are in different timezones.

Try to find out the server locations for both of them and check if you can find something.

Upvotes: 0

Aakash
Aakash

Reputation: 2109

Look here

Link 1 and Link 2.

According to these links, there was a timezone shift happened to this area on 26th Oct 2014, which was taken into account by Java, but not by Oracle DB in your version.

Hopefully, they would have corrected it in the latest one.

Upvotes: 3

Related Questions