【问题标题】:converting date error in grails在grails中转换日期错误
【发布时间】:2013-10-16 10:22:01
【问题描述】:

我喜欢显示我的数据库中的所有记录,仅用于传递日期(我需要传递日期), 我使用 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,我不知道。 我该如何解决这个问题。有什么帮助吗?

【问题讨论】:

  • expenseDate的类型是什么?

标签: java mysql date grails


【解决方案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) SimpleDateFormat 不是线程安全的 2) 为什么 "${value}"??? 3)这段代码会抛出完全相同的错误,值类型没有变化
  • @IgorArtamonov 你试过了吗?
  • 1) 来自javadoc 3) 基本语法,"..." 表示字符串类型
  • 好的,我明白了,您已经修改了答案。但现在它不会编译,因为你不应该在那里使用${}
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    相关资源
    最近更新 更多