简体   繁体   中英

Grails matching dates

I have a string which is:

"may 2013"

This refers to the second of May. I am trying to get all instances of PostOrder where the date is the second of May.

What I have tried so far:

def t = new Date(new Integer(sp[1]), new Integer(month), 01)
def results = PostOrder.createCriteria().list() {
     ilike('dateCreated', t  )
}

Please note I have split the string so sp[1] is 2013 and I have parsed the May so the month variable is not 05.

The above attempt does not work.

The only way I know will work is grabbing all objects and doing a for each on them. parsing the dateCreated to a string then doing a .contains(). But this will get very slow and messy.

First point. According to the documentation , ilike() is a case-insensitive 'like' expression - see SQL LIKE Operator . So, it cannot accept a date. What you need is just eq() .

def t = new Date(new Integer(sp[1]), new Integer(month), 1)
def results = PostOrder.createCriteria().list() {
    eq('dateCreated', t)
}

Another point. The constructor Date() accepts the year minus 1900 as the first argument. So, you probably need to subtract 1900 from new Integer(sp[1]) . Also, that constructor is deprecated; I would suggest to use GregorianCalendar(new Integer(sp[1]), new Integer(month), 1).time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM