Java, Hibernate, MySQL - store UTC date-time -
i saw here on few related questions (like this one , of course, this one)... essentially, want store date-time utc, , let application user choose time zone wants display date-time in.
since seems date-time fields affected underlying jdbc driver, wonder if acceptable way go storing utc date-time:
- set both mysql , application server machine utc time zone (no need separate)
- both mysql , jvm should pick underlying system time settings (if not instructed otherwise)
- use
datetime
table columns on mysql side - use
java.util.date
corresponding mapping on hibernate side (i guessjava.sql.timestamp
used too) - let application worry interpreting date-time fields - i.e. let user choose preferred time zone
is ok?
edit
clarify - here meant refer timestamps created strictly on server (e.g.date-time of record creation). application server instantiates date
objects (new date() equals current date-time on server, , time zone agnostic).
if client user wants supply date searching/filtering purposes, here transformation client-local time utc should take place, imho...
i suggest simple approach independent of machine timezone settings.
- instead of setting timezone of server machine, set timezone of jvm. can done via system properties. on windows example follows
set java_opts=%java_opts% -duser.timezone=gmt
- for mysql here reference doing it.
- now this make sure date-time in gmt
- keep timezone configurable property or can user dependent well. store timezone each user if users belong different geographies.
- whenever, date needed, after select database, apply timezone correct time.
- the advantage of approach work timezone users. meaning user see correct time per timezone.
Comments
Post a Comment