简体   繁体   中英

Grails: error executing tag <g:formatDate>: unknown class: java.lang.String with root cause

I have dates stored in my database in a format like this: 2017-04-12T00:00:00

I am displaying these on an index show page like so:

<td>${event.eventTime}</td>

I want to convert the date into aa regular format, I came across the grails formatDate tag.

I've tried variations of this but the error from the title still remains, where am I going wrong?

<td><g:formatDate date="${event.eventTime }" format="yyyy-MM-dd" /></td>

It appears that eventTime on your event object is a string rather than a date.

If you try the following as a test it should work:

<td><g:formatDate date="${new Date()}" format="yyyy-MM-dd" /></td>

Is eventTime stored as a date in the DB or maybe it's being converted en-route to the gsp?

To just strip off the time & keep the same format you could:

${event.eventTime?.substring(0, 10)}

Or you could convert to a date and back to another format:

<g:formatDate date="${Date.parse( "YYYY-MM-dd'T'hh:mm:ss", event.getTime )}" format="yyyy-MM-dd"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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