我有一个接收具有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()
这并不是由于格式化! 谢谢!
对不起,我试图让后简单,犯了一个错误。我实际上是把格式化程序的结果放入Date中。我编辑了这篇文章。 – iKT