element40
element40

Reputation: 45

How can i convert date in grails?

My Grail application' list view has following date format from database: 2012-01-18 14:29:19 EST

I am using

<g:datePicker name="myDate" value="${new Date().format("dd MM,yyyy")}"/>

and in controller:

def c  = abc.createCriteria()
    def x =  c.list{
            eq("name",params.wfName)
            eq("create_date",params.myDate)
    }

but the list x has nothing inside and return nothing.While i search only but wfName it works perfectly. params.myDate also has date value.

Can anyone please give a point?

Upvotes: 0

Views: 1865

Answers (3)

Daniel Wondyifraw
Daniel Wondyifraw

Reputation: 7723

i have the lates verion of grails but what about other methods of conversion

Upvotes: 0

OverZealous
OverZealous

Reputation: 39580

This might be because the "date" being passed in is a string. Your criteria is looking for an actual date.

You don't specify what version of Grails you are using, but if it's Grails 2.0, they've added methods to params to make the conversion easier:

eq('create_date', params.date('myDate', 'dd MM,yyy'))

Check out this article by mrhaki fore more examples.

For older versions of Grails, you'll have to convert it yourself, using Date.parse(params.myDate, "<< format >>").

Upvotes: 1

doelleri
doelleri

Reputation: 19682

Since Grails 1.2 accessing params.myDate will automatically get you a Date object. If you're not using Grails 1.2 or higher, you'll need to manually create a Date object from the params.

I think the Date you are getting from your datePicker has too much precision to return anything in your query. Try changing your criteria to lt("create_date", params.myDate) to see if you can get any values returned. Also try params.myDate.clearTime() to just leave you with the date.

Upvotes: 0

Related Questions