我如何发现3月2日和3月3日在3月1日和3月4日之间? [英] How do I find that March 2nd and March 3rd falls between March 1st and March 4th?

查看:169
本文介绍了我如何发现3月2日和3月3日在3月1日和3月4日之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1)我有两个日期,分别为 start 。 2)用户在浏览器中选择两个日期,比如 userStartDate userEndDate code>。

我拥有所有这些值,但是我需要编写一个查询来查找 start end 位于 userStartDate userEndDate 之间。例如,3月2日和3月3日是3月1日至3月4日。鉴于:

3月2日和3月3日是 userStartDate userEndDate 日期分别为

3月1日和3月4日是开始结束

code>分别。 (它们是域对象)。



我有这样的代码,它适用于个案之间< start 位于之间 userStartDate userEndDate 如下所示:

  test = Holiday.createCriteria()。list {
和{
user {
eq('username' ,'username')
}
或{
之间('start',userStartDate,userEndDate)
('end',userStartDate,userEndDate)
}
}
}

根据我的问题,如何将该部分附加到我的代码中?



在此先感谢。假设您已经检查过 userStartDate 是否在$ code> userEndDate (在用户选择时验证)并且 start 位于 end

  test = Holiday.createCriteria( ).list {
user {eq('username',username)}
lt('start',userStartDate)
gt('end',userEndDate)
}

这会检查 start 是否小于) userStartDate 并且 end 大于(即之后) userEndDate 。因为所有子句都是隐含的,所以也不需要包装在块中。


I need a way to find these in grails:

1) I have two dates say start and end.

2) User selects two dates in the browser say them userStartDate and userEndDate.

I have all these values, but I need to write a query that do find that both start and end falls between userStartDate and userEndDate.

For example, March 2nd and March 3rd falls between March 1st and March 4th. Given that :

March 2nd and March 3rd are userStartDate and userEndDate dates respectively

March 1st and March 4th are start and end respectively. (they are domain objects).

I have this code which works for between cases i.e start is in between userStartDate and userEndDate like so :

  test = Holiday.createCriteria().list {
            and {
                   user {
                       eq('username',username)
                   }
                   or {
                   between('start',userStartDate,userEndDate) 
                   between('end',userStartDate,userEndDate)
               }
            }
        }

As according to my question, how can attach that part into my code?

Thanks in advance.

解决方案

Assuming that you already do a check that userStartDate is before userEndDate (validated when the user selects) and that start is before end in the database (validated when inserting), the criteria query should look something like this:

test = Holiday.createCriteria().list {
    user { eq('username',username) }
    lt('start', userStartDate)
    gt('end', userEndDate)
}

This checks that start is less than (i.e. before) userStartDate and that end is greater than (i.e. after) userEndDate. There is also no need to wrap in an and block since all clauses are implicitly and-ed.

这篇关于我如何发现3月2日和3月3日在3月1日和3月4日之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆