0

I am getting an exception when I try to format the date which is in oracle.sql.TIMESTAMP. How can format this?

When I try to retrieve the date from the database, I am using the groovy to get the data:

Groovy doc but it doesn't have anything particular to date

     // Part of the code 
      SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS")
        def sql = new Sql(dataSource)
        sql.eachRow('''select dateCreated from User'''){
           row->
           def dateCreated
           if(row.dateCreated != null){ // I am getting the object as oracle.sql.TIMESTAMP.
            dateCreated= df.format(row.dateCreated)
           }
        }

I am getting the exception like this .

2015-11-05 14:22:33,755 [http-8083-3] ERROR errors.GrailsExceptionResolver - Exception occurred when processing request: [GET] /getUsers - parameters: _dc: 1446713474579

Stacktrace follows:

java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:281)
at java.text.Format.format(Format.java:140)
at java_text_Format$format.call(Unknown Source)
at com.test.AdminController$_closure15_closure80.doCall(AdminController.groovy:953)
at $Proxy76.eachRow(Unknown Source)
at com.test.AdminController$_closure15.doCall(AdminController.groovy:941)
at com.test.AdminController$_closure15.doCall(AdminController.groovy)
at com.test.SecureController.invokeMethod(SecureController.groovy)
at com.test.AdminController$_closure21.doCall(AdminController.groovy:1061)
at com.test.AdminController$_closure21.doCall(AdminController.groovy)
at org.grails.jaxrs.web.JaxrsFilter.doFilterInternal(JaxrsFilter.java:46)
at java.lang.Thread.run(Thread.java:662)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ajayramesh
  • 3,576
  • 8
  • 50
  • 75

2 Answers2

1

If it is oracle.sql.TIMESTAMP you are talking about, take a look here http://docs.oracle.com/cd/E18283_01/appdev.112/e13995/oracle/sql/TIMESTAMP.html

This class has a lot of usefull methods to convert it to java.sql.Date (dateValue()) or java.sql.Timestamp (timestampValue()) etc.

Once you get to those it's pretty easy to convert them to java.util.Date. For example take a loo here How to convert from java.sql.Timestamp to java.util.Date?

Community
  • 1
  • 1
Ashraf Purno
  • 1,065
  • 6
  • 11
1

Cannot format given Object as a Date , this error clearly indicates that first you need to typecast the result you are getting to date and then format it.

Date date = Date.parse("yyyy/MM/dd HH:mm:ss",row.dateCreated)

This should work.

tim morgan
  • 21
  • 2