mateusmaso
mateusmaso

Reputation: 8473

how to change: Sun Jan 01 00:00:00 BRST 2006 to 2006-01-01 00:00:00.0

Hey guys, how can I tranform the format of datePicker that came to my action params?

My actual Date attribute doesn`t accept this"Sun Jan 01 00:00:00 BRST 2006" type, only "2006-01-01 00:00:00.0 " for example.

How can I deal with it?

Upvotes: 0

Views: 274

Answers (2)

Pietro
Pietro

Reputation: 1836

Why you don't use Joda-time instead of Date that has many deprecate methods, and is less powerful. In the gsp file then you just need to use: .toString("format")

Upvotes: 0

robbbert
robbbert

Reputation: 2193

It's hard to investigate in questions that don't provide required information to reproduce the issue. In your case, that would be the error type, and message, and the related code snippets, at least. -

Normally, your datePicker result "Sun Jan 01 00:00:00 BRST 2006" should be perfectly fine with standard code like that:

Controller action method:

def index = {
    Date date = params.datePicker
    [date: params.datePicker ?: new Date()]
}

GSP:

<g:datePicker name="datePicker" value="${date}" />

Now let's go on to wild guesses:

In case you're trying to parse that date string using the SimpleDateFormat class, the corresponding conversion pattern would be:

Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
    .parse("Sun Jan 01 00:00:00 BRST 2006")

resp.,

Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
    .parse(params.datePicker)

It's also possible to register a CustomDateEditor, but there shouldn't be a need to do with a datePicker.


After all, the datePicker result should be converted to a Date, automatically, as depicted in the first sample. - If it still does not, please clarify your issue.

Upvotes: 3

Related Questions