Search code examples
mysqlgrailsgrails-orm

Grails insert date without time


I have a date attribute in my domain and I want to insert to MySQL without time. getting exception cannot cast string to date.Exception message:

Cannot cast object 2013-03-09 with class java.lang.String to class java.util.Date

I want to insert to the database without time.

Domain:

class Day {

    
Date date
    
        static mappings = {
        table name:'Days'
   
              date type: 'date'
         
         
    }

Controller:

def today = new Date()
def ymdFmt = new java.text.SimpleDateFormat("yyyy-MM-dd")
Date dateYmd = ymdFmt.format(today) 



        day.date =dateYmd 

Solution

  • I think you wan't to look at Date.parse http://groovy.codehaus.org/groovy-jdk/java/util/Date.html#parse(java.lang.String, java.lang.String)

    You can parse the date like.

    String stringDate = '28/09/2010'
    Date date = new Date().parse('d/M/yyyy', stringDate)