2012-01-23 86 views
1

我有一个接收具有NSDate的JSON请求的grails服务器。我收到的日期如下格式:NSDate日期字符串转换为java.util.date

2012-01-23 4时47分27秒+0000

我需要将其转换为Java数据格式能够存储。我做了转换以下:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); 
Date date = (Date)formatter.parse(request.JSON.StartDate); 

格式化的日期出来为:太阳1月22日20时47分27秒的PST 2012

当我尝试做一个保存(),我得到以下错误:

org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'com.test.date' on field 'StartDate': rejected value [Sun Jan 22 20:47:27 PST 2012]; codes [typeMismatch.com.test.date.eventStartDate,typeMismatch.StartDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [com.test.date.StartDate,StartDate]; arguments []; default message [StartDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'StartDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'StartDate': no matching editors or conversion strategy found]

任何人都可以告诉我这有什么问题,我需要看什么?

编辑:

其实我得到的错误,甚至当我做到以下几点:

object.date = new Date() 
object.save() 

这并不是由于格式化! 谢谢!

回答

1

我不认为你的代码编译如图所示,但无论如何它看起来像你格式化日期,然后立即存储(并最终试图保存)其字符串表示。只是删除多余的不必要的转换为字符串:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); 
Date date = formatter.parse(request.JSON.StartDate); 
+0

对不起,我试图让后简单,犯了一个错误。我实际上是把格式化程序的结果放入Date中。我编辑了这篇文章。 – iKT

0

UnBelievable !!!!我在这个问题上挣扎了两天,事实证明我正在使用的Grails域类属性名称是问题。我之前使用“eventStartDate”,当我将其更改为“DOB”时,它开始正常工作。

我很新的Grails和不是很肯定的命名限制,但我学到了很好的教训不能忽视的允许命名;)

相关问题