1

我目前正在将 Joda DateTime (Datetime+TimeZone) 值存储到数据库中。

我想做的是根据用户的时区而不是 UTC 时间来获取 24 小时的记录。IE 我想为时区为 GMT 的人获取 00:00GMT-24:00GMT 之间的记录,为时区为 PST 的人获取 00:00PST-24:00PST 之间的记录。

现在我认为我不能在 HQL 查询中执行此操作,因为显然 HQL 不会为我执行 DateTime.toDateTime(toZone) 转换。那么是否可以在 groovy/grails 中执行这种范围搜索?

4

1 回答 1

1

我想了更多,解决办法可能是用另一种方式攻击它。与其尝试在休眠级别执行 toDateTime(zone),不如在更高级别执行 - 即在创建开始->结束搜索条件时;

    DateTimeZone toZone = DateTimeZone.forID("Europe/London");
    DateTime now = new DateTime()
    DateTime today = new DateTime(now.year, now.monthOfYear, now.dayOfMonth, 0, 0, 0, 0)
    DateTime yesterday = today.minusDays(1)

    def queryStr = "from JODATesting j where j.thedate > :date1 and j.thedate < :date2"
    for (JODATesting joda: JODATesting.executeQuery(queryStr, [date1: yesterday, date2: today]) ) {
        println("##### Joda"+joda.id +"=" +joda.thedate.toDateTime(toZone));
    }
于 2010-03-24T17:22:59.500 回答