How to configure correct timezone in JDBC?

江枫思渺然 提交于 2019-12-10 10:44:12

问题


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?


回答1:


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 the long of the number of milliseconds since the epoch, e.g. from System.currentTimeMillis() or Date.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:

SELECT FROM_UNIXTIME(date_field / 1000) FROM table;



回答2:


There is a list of "tz" timezone names in Wikipedia.



来源:https://stackoverflow.com/questions/5399563/how-to-configure-correct-timezone-in-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!