我喜欢显示数据库中仅传递日期的所有记录(我需要传递日期), 我使用 grails calander 1.2.1 插件 在我的数据库中,日期存储为2013-10-16 12:19:00, 在我的 Controller 中我写了
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,我不太清楚。 我该如何解决这个问题。有什么帮助吗?
最佳答案
解决方案之一:只需在 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;
}
}
在 Controller 中使用它之后。
def result=DailyBudget.createCriteria().list()
{
eq("expenseDate", MyUtils.toDate(params.expenseDate_value))
}
关于java - grails中转换日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19400891/