转换grails中的日期错误

时间:2013-10-16 10:22:02

标签: java mysql date grails

我想显示我的数据库中的所有记录仅传递日期(我需要通过日期), 我用grails calander 1.2.1插件  在我的数据库日期存储如 2013-10-16 12:19:00, 在我的控制器中我写了

println"passed date is"+params.expenseDate_value            //its prints 20/3/2013 

def result=DailyBudget.createCriteria().list()

    {
        eq("expenseDate","params.expenseDate_value")
    }
println"result"+result

这里 params.expenseDate_value 代表我的传递日期,当我打印它显示我的传递日期,运作良好。而在标准 我得到这样的错误 java.lang.String无法强制转换为java.util.Date 我认为 expenseDate 获得pbm,我完全不知道。  我该如何解决这个问题。任何帮助PLZ?

2 个答案:

答案 0 :(得分:1)

其中一个解决方案:只需在src/groovy文件夹下创建简单的groovy类。使用以下代码。

class MyUtils() {
      private static DateFormat dateFormat = new SimpleDateFormat("/*date format*/")

      public static Date toDate(Object obj) {             
         if (obj) {
           return dateFormat.parse(obj)
         }
         return null;
      }

}

在您的控制器中使用它之后。

def result=DailyBudget.createCriteria().list()

    {
        eq("expenseDate", MyUtils.toDate(params.expenseDate_value))
    }

答案 1 :(得分:0)

您没有回答,但我相信您的expenseDateDate字段。因此,只需将String转换为Date,例如:

{
    eq("expenseDate", Date.parse('MM/dd/yyyy', params.expenseDate_value))
}