0

如果您在 toad 中针对 oracle DB 运行此查询:

select 
to_char(TRUNC(sysdate, 'DAY'),'DAY') start_of_the_week
from dual;

根据语言环境服务器设置,您将获得:

Sunday --EN, USA
Monday -- DE, GERMANY

这是完全有道理的,没关系。

如果您通过 OJDBC 驱动程序在 Java 中执行完全相同的操作,如下所示:

//....
Statement stm = JDBCConn.createStatement();
stm.execute("select to_char(TRUNC(sysdate, 'DAY'),'DAY') start_of_the_week from dual");
stm.getResultSet().next();
System.out.println(stm.getResultSet().getString("start_of_the_week"));

您将返回周日或周一,但这取决于您提供给运行此语句的 JVM 的客户端本地设置。这意味着您得到的答案不再取决于服务器本地设置,而是取决于您的客户端设置。

修复非常简单,添加

Locale.setDefault(new Locale("EN", "US", "WIN")); //or German etc..

现在到我的实际问题。为什么会有人认为这是个好主意?!?有没有一种情况为什么这是有道理的?这是一个功能吗,因为在我看来这是一个错误,或者至少是非常糟糕的设计/概念。

问候,科林

4

1 回答 1

0

this is supposedly jdbc driver feature

be careful Locale.setDefault - sets jvm default, that would affect other parts of your application

with oracle you also have to be careful when handling timestamp kind of columns and how to map them to java

See http://tonyhasler.wordpress.com/2010/09/04/tonys-tirade-against-timestamp-with-time-zone/

Daylight saving time and time zone best practices

In the Oracle JDBC driver, what happens to the time zone when you write a Java date to a TIMESTAMP column?

于 2014-08-13T17:47:16.503 回答