【问题标题】:Grails : how to save java.sql.Time type in sql databaseGrails:如何在 sql 数据库中保存 java.sql.Time 类型
【发布时间】:2015-01-16 13:00:06
【问题描述】:

我在名为 config 的表中添加了一个新的时间类型列,该列由 Grails 在我的数据库中生成一个日期时间类型。

这是我的域类

import java.sql.Time
class Config {
      int nbr_delay
      Time max_duration
      static constraints = {
      }
}

在我的 GSP 页面中

<g:field type="time" name="max_duration" value="${fieldValue(bean: configInstance, field: 'max_duration')}" />

现在,当我尝试更改 max_duration 的值并将其保存在数据库中时,它会显示此错误 Failed to convert property value of type java.lang.String to required type java.sql.Time for property max_duration; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "00:30"

我的控制器

configInstance.properties = params
configInstance.save(flush: true)

我搜索了如何解析或操作这种数据类型,但没有找到任何解决方案。

【问题讨论】:

    标签: sql-server grails grails-orm datetime-format


    【解决方案1】:

    我会说,如果您有 duration 字段,请使用 intlong 来保存分钟或毫秒:

    import java.sql.Time
    class Config {
    
      long maxDuration
    
      void setMax_duration( Time time ){ maxDuration = time.time }
    
      Time getMax_duration(){ new Time( maxDuration ) }
    
      static transients = [ 'max_duration' ]
    }
    

    然后你可以在你的 GSP 中做:

    <g:formatDate format="HH:mm" date="${new Date( configInstance.maxDuration )}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 2015-02-24
      相关资源
      最近更新 更多