且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数据库中保存时区信息?

更新时间:2022-10-25 08:41:32

有些数据库有类型包括时区信息(例如Oracle具有TIMESTAMP WITH TIME ZONE)。尽可能使用这些。否则,请将所有日期另存为GMT和时区。

至于时区的格式,***使用 tz数据库,这是Java的时区ID所基于的,例如America / New_York或Europe / London。这些ID唯一标识(不像三个字母的时区名称通常有多重含义)管理时区及其历史记录(包括DST引入/取消和更改) - 所有这些都可能是正确解释日期所必需的。


I am using GWT/Hibernate. i have a form created using GWT. it has one date field where user can select a date and one drop down where all timezones are displayed. now user selects date and timezone from dropdown. on click of save button i need to save date and timezone information. Also if server is configured in GMT, if user has selected any other timezone then it should be saved as user selected timezone information. when i try to query the same then it should return me the date with user selected timezone. Please suggest me what is the best way to implement it? should i have date and timezone columns in db separately? in which format do i need to save timezone information? Please help me.

Thanks!

Some databases have types that include timezone information (e.g. Oracle has TIMESTAMP WITH TIME ZONE). Use those if possible. Otherwise, save all dates in GMT and the timezone separately.

As for the format of the timezone, it's best to use the IDs from the tz database, which Java's timezone IDs are based on, e.g. "America/New_York" or "Europe/London". These IDs uniquely identify (unlike the three-letter timezone names which often have multiple meanings) an administrative timezone and its history (including DST intriduction/abolition and changes) - all of which can be necessary to correctly interpret a date.