質問

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?

役に立ちましたか?

解決

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.

他のヒント

you can use

Date vDate = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(query.toString())

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top