简体   繁体   English

Java,Hibernate,MySQL-存储UTC日期时间

[英]Java, Hibernate, MySQL - store UTC date-time

I saw here on SO a few related questions (like this one and of course, this one )... Essentially, what I want is to store date-time as UTC, and let application user choose the time zone he wants to display date-time in. 我在这里看到了一些相关的问题(例如这个 ,当然还有这个 )……本质上,我想要的是将日期时间存储为UTC,并让应用程序用户选择他想显示日期的时区时间。
Since it seems that date-time fields are affected by the underlying JDBC driver, I wonder if this is an acceptable way to go about storing UTC date-time: 由于日期时间字段似乎受到底层JDBC驱动程序的影响,所以我想知道这是否是可以接受的存储UTC日期时间的方法:

  • Set both MySQL and Application server machine to UTC time zone (no need to separate) 将MySQL和Application Server计算机都设置为UTC时区(无需分开)
  • Both MySQL and JVM should pick up underlying system time settings (if not instructed otherwise) MySQL和JVM都应该选择基础系统时间设置(如果没有其他说明的话)
  • Use DATETIME table columns on MySQL side 在MySQL端使用DATETIME表列
  • Use java.util.Date as corresponding mapping on Hibernate side (I guess java.sql.Timestamp could be used too) 使用java.util.Date作为Hibernate端的对应映射(我想也可以使用java.sql.Timestamp
  • Let the application worry about interpreting date-time fields - ie let the user choose preferred time zone 让应用程序担心解释日期时间字段-即让用户选择首选时区

Is this OK? 这个可以吗?

EDIT 编辑
To clarify - here I meant to refer to timestamps created strictly on the server (egdate-time of record creation). 为了澄清-这里我指的是严格在服务器上创建的时间戳(例如,记录创建的日期)。 So the application server instantiates Date objects (new Date() equals current date-time on the server, and this is really time zone agnostic). 因此,应用程序服务器实例化Date对象(新的Date()等于服务器上的当前日期时间,这实际上是时区不可知的)。
Now if a client user wants to supply some date for searching/filtering purposes, here is where the transformation from client-local time to UTC should take place, IMHO... 现在,如果客户用户想要提供一些日期以进行搜索/过滤,则应在此处进行从客户本地时间到UTC的转换,恕我直言...

I would suggest another simple approach which would independent of machine timezone settings. 我建议另一种简单的方法,该方法与计算机时区设置无关。

  • Instead of setting the timezone of the server machine, set the timezone of JVM. 代替设置服务器计算机的时区,而是设置JVM的时区。 This can be done via system properties. 这可以通过系统属性来完成。 On Windows example would as follows 在Windows上的示例如下
 set JAVA_OPTS=%JAVA_OPTS% -Duser.timezone=GMT 
  • For MySQL here is the reference for doing it. 对于MySQL, 这里是执行此操作的参考
  • Now this this make sure that all your date-time are in GMT 现在,这可以确保您所有的日期时间都在格林尼治标准时间
  • Keep the timezone as configurable property OR it can be user dependent as well. 保持时区为可配置属性,也可以取决于用户。 So you store timezone for each user if the users belong to different geographies. 因此,如果用户属于不同的地理位置,则可以为每个用户存储时区。
  • Whenever, a date is needed, after you select it from the database, apply the timezone to get the correct time. 每当需要日期时,从数据库中选择日期后,请应用时区以获取正确的时间。
  • The advantage of this approach is that this will work for the all the timezone users. 这种方法的优点是,它适用于所有时区用户。 Meaning the user will see the correct time as per their timezone. 这意味着用户将按照他们的时区看到正确的时间。

使用locales来实现国际化。

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

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