简体   繁体   English

JDBC 不返回与系统相同的时区

[英]JDBC not returning same time zone as system

My database is set to default time zone UTC.我的数据库设置为默认时区 UTC。 My Driver should be using system time which I am running my application in a ubuntu container that is set to UTC.我的驱动程序应该使用我在设置为 UTC 的 ubuntu 容器中运行我的应用程序的系统时间。 I know this because when I call Timezone.getDefault() is returns UTC.我知道这一点,因为当我调用 Timezone.getDefault() 时返回 UTC。 So according to my understanding, these times should be close to identical +-1 hour.所以根据我的理解,这些时间应该接近相同的 +-1 小时。 I am currently in CST.我目前在 CST。 The database time is retrieved with resultset.getTimestamp().getTime().使用 resultset.getTimestamp().getTime() 检索数据库时间。 One (or two) of these numbers are not UTC.这些数字中的一个(或两个)不是 UTC。

Database time: 1674680930000
System.currentTime(): 1674703050253
Calendar time: 1674703050255

My database is set to default time zone UTC.我的数据库设置为默认时区 UTC。

Write your Java code in such a way that you don't care about the current default time zone of the server OS nor the current default time zone of the database session.以这样一种方式编写您的 Java 代码,即您不关心服务器操作系统的当前默认时区或数据库的当前默认时区 session。

Edit your Question to provide actual example code.编辑您的问题以提供实际的示例代码。 Then we can critique.然后我们可以批评。

My Driver should be using system time which I am running my application in a ubuntu container that is set to UTC.我的驱动程序应该使用我在设置为 UTC 的 ubuntu 容器中运行我的应用程序的系统时间。

No, incorrect.不,不正确。 Your JDBC driver is unaware of the host OS' current default time zone.您的JDBC 驱动程序不知道主机操作系统的当前默认时区。

What makes you think otherwise?是什么让你有不同的想法? Edit your Question to provide details.编辑您的问题以提供详细信息。

I know this because when I call Timezone.getDefault() is returns UTC.我知道这一点,因为当我调用 Timezone.getDefault() 时返回 UTC。

That code gets the current default time zone of your JVM .该代码获取JVM的当前默认时区。 Your JVM's current default time zone may or may not be the same as host OS' the current default time zone.您的 JVM 当前默认时区可能与主机操作系统的当前默认时区相同,也可能不同。 The two defaults can be set independently of one another.这两个默认值可以相互独立设置。

TimeZone is one of the legacy date-time classes that you should avoid. TimeZone是您应该避免使用的遗留日期时间类之一。 Use only java.time classes.仅使用java.time类。 Never use Dote , Calendar , Timestamp , etc.切勿使用DoteCalendarTimestamp等。

To determine your JVM's current default time zone:要确定 JVM 当前的默认时区:

ZoneId z = ZoneId.systemDefault() ;

I am currently in CST.我目前在 CST。

As I said above, you should write your Java code to avoid depending on any default time zone.正如我上面所说,您应该编写 Java 代码以避免依赖于任何默认时区。 Pass optional parameters with your desired/expected time zone.使用您想要/预期的时区传递可选参数。

Record the current moment as seen in UTC.记录 UTC 中看到的当前时刻。

OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;
myPreparedStatement.setObject( … , odt ) ;

Retrieve from database.从数据库中检索。

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

Adjust to your desired time zone.调整到您想要的时区。 CST is not a real time zone . CST不是实时时区 Perhaps you meant America/Chicago .也许你的意思是America/Chicago

ZoneId z = ZoneId.of( "America/Chicago" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

The database time is retrieved with resultset.getTimestamp().getTime().使用 resultset.getTimestamp().getTime() 检索数据库时间。

Incorrect.不正确。 The ResultSet class has no getTimestamp method taking no arguments. ResultSet class 没有获取 arguments 的getTimestamp方法。

Perhaps you mean to retrieve a date-time value from a column.也许您的意思是从列中检索日期时间值。 How to do that depends on the data type of the column.如何做到这一点取决于列的数据类型。

Edit your Question to document the exact data type of your column, the database engine, and the version.编辑您的问题以记录您的列的确切数据类型、数据库引擎和版本。

For MySQL 8 :对于MySQL 8

SQL standard SQL标准 MySQL MySQL Java Java
TIMESTAMP WITH TIME ZONE TIMESTAMP OffsetDateTime
TIMESTAMP WITHOUT TIME ZONE DATETIME LocalDateTime

And search Stack Overflow to learn more.搜索 Stack Overflow以了解更多信息。 Exchanging date-time values with a database has been covered extensively.与数据库交换日期时间值已被广泛涵盖。

You commented:你评论说:

So it looks like I missed an important distinction between session and global env variables in MySQL:所以看起来我错过了 session 和 MySQL 中的全局环境变量之间的重要区别:

Neither of those need be relevant to your Java code for exchanging date-time values with a database.这些都不需要与用于与数据库交换日期时间值的 Java 代码相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM