wrong timezone when loading joda Instant field via JPA
In my Play application I have a model with a created
field of type Instant
. It's populated using a @PrePersist
hook which just sets it to a new Instant()
. The default timezone is correctly detected as US/Chicago
, and when persisted to mysql it appears in the same timezone (in a datetime
field).
However, when the object is read back from the database and the time is displayed, it's shown in UTC rather than local time. I'm currently displaying it using
${org.joda.time.format.DateTimeFormat.forStyle("SS").print开发者_如何学JAVATo(out, object.created)}
Being new to Joda and fairly new with JPA, I'm not sure if this is a joda problem, a JPA problem, if I should be using a different Joda type, a different mysql type, or if I'm going about this completely wrong. Can someone point me in the right direction?
According to the API UTC seems to be the reference timezone. You can always change that on the DateTimeFormat itself with the withZone(DateTimeZone)
method. The following code outputs the date in America/Chicago which seems to be the right timezone in joda-zime:
${org.joda.time.format.DateTimeFormat.forStyle("SS").withZone(org.joda.time.DateTimeZone.forID("America/Chicago")).printTo(out, object.created)}
Following the user guide you can also set the default timezone for joda with DateTimeZone.setDefault(DateTimeZone.forID("America/Chicago"));
but I always thought that specifing the default timezone with an VM argument works too and should be the better option.
精彩评论