Will
Will

Reputation: 482

Check if current date lies between start date and end date

I am doing some work in Grails. My domain class (Simple Messages) consists of a start date and end date. I want to display to the user the 'messages' only if the current date (now) is within the start date and end date, i.e. the message should be visible to the user only if today is between start date and end date. Please also note, I do not want any time related computation.

Here is the domain class:

class SimpleMessage
{
    String message
    Date startDate
    Date endDate
}

Here is the method defined in my service to extract valid messages:

def GetLiveMessages()
{
    def coTimeStamp = new Date().clearTime().toTimestamp()

    def c = SimpleMessage.createCriteria()

    def coCurrentMessages = c.list
    {
        ge("startDate", coTimeStamp) 
        le("endDate", coTimeStamp)
    }

    return coCurrentMessages
}

coCurrentMessages is always returning with size 0. I have added records (start date - 1 June 2012 and end date 2 June 2012) with valid dates, still this returns 0 (for today - 1 June 2012). Surprisingly, if i remove the 2nd constraing 'le' I get the message record correctly.

Your help will be appreciated.

Upvotes: 1

Views: 1230

Answers (1)

JB Nizet
JB Nizet

Reputation: 691785

If the API used above is similar to the native Hibernate Restrictions API (and I guess it is, because it would be more logical), you just used ge when you should have used le, and vice-versa:

le("startDate", coTimeStamp) // startDate <= current date
ge("endDate", coTimeStamp) // endDate >= current date

Upvotes: 4

Related Questions