解析JSON日期

时间:2013-01-12 06:21:39

标签: grails groovy grails-2.0

我使用Grails作为服务器,使用ExtJS作为客户端。现在我需要使用JSON中的特定日期值更新域的日期字段。但是当我想用特定日期更新时,错误就像这样

  

无法将类'java.lang.String'的对象'dd MM,yyyy'强制转换为类'java.util.Date'

现在我如何更新JSON到数据库的具体日期?这是我的服务器更新操作:

def update(json) {
    def count = 0
    def flag = true
    def installments = []
    def pdate = json.payDate
    def payDate = pdate.format("dd MM, yyyy")
    def newCashPay = Double.parseDouble("${json.cashPay}")
    def newReturnAmount = Double.parseDouble("${json.returnAmount}")
    if(json instanceof JSONObject){            
        def c = Installment.get(Integer.parseInt("${json.id}"))
        if(c){   
            c.activeFlag = true
            c.cashPay = newCashPay
            c.returnAmount = newReturnAmount
            c.payDate = payDate
            if(c.validate()){
                c.save()
            }else{                        
                c.errors.each {
                    println it
                }
            }
        }

        def installment = [id:c.id, accNo:c.accNo, memberNo:c.memberNo, branchId:c.branchId, installmentNo:c.installmentNo, weekStart:c.weekStart, weekEnd:c.weekEnd, payDate:c.payDate, payAmount:c.payAmount, lateFee:c.lateFee, cashPay:c.cashPay, returnAmount:c.returnAmount, activeFlag:c.activeFlag]
        installments << installment
        flag = flag && true
        count++
    }
}

1 个答案:

答案 0 :(得分:3)

而不是

def payDate = pdate.format("dd MM, yyyy")

你不是说

def payDate = Date.parse( "dd MM, yyyy", pdate )