如何在 JDBC 中配置正确的时区?
我有这个网址来在我的意大利网站中设置连接,但是,当我尝试从该网站执行某些插入操作时,日期仍然不正确。 (例如应该是:01:24,但现在是 02:24)
jdbc.url=jdbc:mysql://sql.example.com/database?autoReconnect=true&characterEncoding=UTF-8&sessionVariables=time_zone='Europe/Rome'
我是否需要添加任何其他参数才能使其正常工作? 是否有所有时区的完整列表?
I've this url to set up the connection in my Italy website, however, when i try to perform some insert action from the site, the date is still not right. (it should be for example: 01:24, but it is 02:24)
jdbc.url=jdbc:mysql://sql.example.com/database?autoReconnect=true&characterEncoding=UTF-8&sessionVariables=time_zone='Europe/Rome'
Do I need to add any other params to make it work correctly?
Is there a complete list of all timezones?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,我无法回答您的直接问题。不过,我可以建议一些值得考虑的事情,这将完全避免数据库中的所有时区问题。如果可能的话,我建议简单地使用
BIGINT
字段来通过 Java 存储日期。您只需存储自纪元以来的毫秒数的long
,例如来自System.currentTimeMillis()
或Date.getTime()
。然后,日期时区的解释始终在 Java 中管理,Java 擅长使用基于纪元的数字。它确实使在 Java 之外直接查询数据库中的日期变得更加复杂,但是这并不太难,而且在我看来是值得的:
Sorry I don't have the answer to your direct question. However I can suggest something worth considering that will avoid all time zone problems at the database entirely. If possible I recommend simply using
BIGINT
fields for storing dates with Java. You just store thelong
of the number of milliseconds since the epoch, e.g. fromSystem.currentTimeMillis()
orDate.getTime()
.Then interpretation of the time zone for a date is always managed in Java, which is good at using the epoch based number. It does make it a little more involved to directly query the database for a date outside of Java, however it's not too hard and tends to be worth it IMO:
维基百科中有一个“tz”时区名称列表。
There is a list of "tz" timezone names in Wikipedia.