1

所以这就是我想要做的。我有一个 gsp 视图,其表单发布了以下带有一些示例值的内容:

params.startTime: 01:00:00 AM, params.startDate: 2013-01-01, params.timeZone: US/Central

这是我的域类中的一些代码

    class MyClass {
     DateTime startDate
    }
    static mapping {
     startDate type: PersistentDateTimeWithZone, {
      column name: "start_date_time"
      column name: "start_date_zone"
    }

这是我的控制器的一些代码

    //This is a service i created to return the UTC id of a long time zone
    def tzs = new TimeZoneService()
    params.timeZone = tzs.getTimeZoneId(params.timeZone)    
    def startTimeZone = DateTimeZone.forID(params.timeZone)

    def startDate = new DateTime(params.startDate + "T" + params.startTime, startTimeZone)

这是我的数据库中的样子:

    start_date_time: 2013-01-01 01:00:00 
    start_date_zone: -06:00

所以这就是问题所在。当我使用以下模式格式化 startTime

    DateTimeFormatter fmt = DateTimeFormat.forPattern("MMM dd, yyyy  h:mm a z");

我的时区显示为 -06:00 而不是美国/中部。这是我的问题:我正在尝试不同的方法来持久化我的 DateTime。如果我只想使用一列将日期时间和区域保持在一起,我该怎么做?如果我的日期时区属性作为单独的参数出现,那么使用这三个参数构建 DateTime 的正确方法是什么?请帮忙!提前致谢。

4

1 回答 1

0

为了修复此错误,您需要获取时区偏移的 UTC 值。我相信它通过 Joda DateTimeZone 为您提供的输出是 America/Chicago、America/Eastern、America/Pacific 等,因此,如果您想要自定义值,例如 US/Central,则必须创建一个映射您想要匹配这些偏移量的任何值。

希望这可以帮助!

于 2013-03-15T15:43:32.743 回答