简体   繁体   English

请求验证 - 数据库和Java如何存储和处理时区?

[英]Request to Verify - How Databases and Java store and handle timezones?

My apologies for the extremely basic question, I think I know the answer but would like to verify: 我对这个非常基本的问题表示道歉,我想我知道答案,但想验证:

When refering to time zones and how they are usually stored in a database and in a java.util.Date: 在引用时区以及它们通常如何存储在数据库和java.util.Date中时:

  • When saving a date field in a database, it is timezone agnostic (eg always saves in UTC) 在数据库中保存日期字段时,它与时区无关(例如始终以UTC格式保存)
  • When using a Java Date object the date it is also timezone agnostic 使用Java Date对象时,它也是时区不可知的
  • Time zone is depeneded only when formatting and parsing dates (DB and Java) 只有在格式化和解析日期(DB和Java)时才会定义时区
  • If using Java - it will use the JVM user.timezone to format / parse dates 如果使用Java - 它将使用JVM user.timezone来格式化/解析日期
  • If using Java on Windows - Java will take this from the Regional settings automatically 如果在Windows上使用Java - Java将自动从区域设置中获取此信息
  • The database timezone (and it's machine's timezone) is irrelevant to the Java JDBC client 数据库时区(及其机器的时区)与Java JDBC客户端无关
  • The timezone of the Database Server is relevant only for direct SQL parsing and formating 数据库服务器的时区仅与直接SQL解析和格式化相关

My questions are: 我的问题是:

  1. Are all of the above assumptions correct? 所有上述假设都是正确的吗? some are? 有些是? all incorect? 所有的incorect?
  2. Is there a reference / official source that verifies this more "officially"? 是否有参考/官方消息来源更“正式”验证?

The assumption are mostly correct for Java. Java的假设大多是正确的。 They are not necessarily correct for databases, as there are variations. 它们不一定适用于数据库,因为存在变化。

Java handles time zones with Calendar objects. Java使用Calendar对象处理时区。 The java.util.Date object always contains the UTC value. java.util.Date对象始终包含UTC值。

Databases generally store and return dates and timestamps (with hour, minutes, etc.) as they are written, regardless of the internal format used by storage. 无论存储使用何种内部格式,数据库通常会在写入时存储和返回日期和时间戳(包括小时,分钟等)。 If you store 2010-12-25, you will retrieve the same value regardless of the time zone of the clients or server. 如果存储2010-12-25,则无论客户端或服务器的时区如何,都将检索相同的值。

Some databases have the TIMESTAMP WITH TIMEZONE data type which stores both the timestamp and the time zone separately. 某些数据库具有TIMESTAMP WITH TIMEZONE数据类型,它分别存储时间戳和时区。

Dates and timestamps are converted between Java and Database, usually in a manner that the subclasses of java.util.Date that are used are interpreted in the JDBC client's time zone. 日期和时间戳在Java和数据库之间转换,通常以在JDBC客户端的时区中解释所使用的java.util.Date的子类的方式进行转换。

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

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