-1

I use Grails 2.0.3 and am comparing a a date string post value using creatCriteria and i have used below methods to convert the string into date but it always empty ? when i try , i can use params.date() method since i pass only one string ,for searching it can be date in one time ,or it can be number and so on ...

 println Date.parse('2013-02-05') 

my creatCriteria

 def vDate = new Date().parse(query.toString())

   {

  eq('dateCreated',vDate) 

   }

what am missing or any alternative ? i still believe that this trivial issue has to be resolved enough for future use?

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
  • FYI you can do this: `params.date("yourField", "yyyy-MM-dd")`. You can also use HQL and `trunc()` the date on the DB. – James Kleeh Jun 11 '13 at 16:50

3 Answers3

1

you can use

Date vDate = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(query.toString())
  • newdate = new java.text.SimpleDateFormat("yyyy-MM-dd").parse("2013- 05-24") println "NEW DATE TOP "+ newdate – Develop4Life Jun 11 '13 at 13:08
  • the above doesn't work returntinf the perfect formatt i expect date value of 2013-05-24 ,but it rather display "NEW DATE TOP Fri May 24 00:00:00 EAT 2013" – Develop4Life Jun 11 '13 at 13:09
  • 1
    earlier you said that you want to parse date in order to use it criteria, right? then using my code you can get a Date object, if you print it it uses toString() method which produces date in that format, you can use SimpleDateFormat class again for formatiing it to yyyy-MM-dd format if you want, see [javadoc](http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html) – Kamil Mikolajczyk Jun 11 '13 at 14:17
  • Yes Se Dude it works fine my bad , tricked by to string methods actually it works with the database comparison too! thanks@ – Develop4Life Jun 11 '13 at 15:08
1

If your database column is of type Date then you should not have problem using the way @Kamil has suggested. Although, there is a groovier way of parsing the date string to date.

def date = Date.parse('yyyy-MM-dd','2013-02-05')
assert date instanceof java.util.Date
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
1

First of all convert your String to date using this method.

def date = Date.parse('yyyy-MM-dd','2013-02-05')

Now use today start and today end method.

Date getTodayStart( Date inDate){
Calendar cal = Calendar.getInstance()
    cal.set(inDate[Calendar.YEAR], inDate[Calendar.MONTH], inDate[Calendar.DATE], 0, 0, 0)
    Date todayStart = cal.getTime()

    return todayStart
}

Date getTodayEnd(Date inDate){
Calendar cal = Calendar.getInstance()
    cal.set(inDate[Calendar.YEAR], inDate[Calendar.MONTH], inDate[Calendar.DATE], 0, 0, -1)
    Date todayEnd = cal.getTime() + 1

    return todayEnd
}

It will give you object from day start to day end.

Date startDate = getTodayStart(date)
Date endDate =  getTodayEnd(date)

Now you can create criteria for date.

  {
  between('dateCreated',startDate.toString(),endDate.toString()) 
  }

It will give you all date for that day.

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
user1791574
  • 1,729
  • 2
  • 16
  • 30