简体   繁体   English

Grails匹配日期

[英]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. 我正在尝试获取日期为5月2日的PostOrder的所有实例。

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. 请注意,我已经分割了字符串,所以sp [1]是2013,并且我已经解析了May,所以month变量不是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(). 将dateCreated解析为字符串,然后执行.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 . 根据文档ilike()是不区分大小写的“ like”表达式-请参见SQL LIKE Operator So, it cannot accept a date. 因此,它不能接受日期。 What you need is just eq() . 您需要的只是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. 构造函数Date() 接受年份减去1900作为第一个参数。 So, you probably need to subtract 1900 from new Integer(sp[1]) . 因此,您可能需要从new Integer(sp[1])减去1900 Also, that constructor is deprecated; 另外,不建议使用该构造函数; I would suggest to use GregorianCalendar(new Integer(sp[1]), new Integer(month), 1).time . 我建议使用GregorianCalendar(new Integer(sp[1]), new Integer(month), 1).time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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