budjb / grails-jaxrs

JAX-RS Plugin for Grails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with date in JSON if hour is 12 like : 2014-01-28T12:56:37Z

budjb opened this issue · comments

From @hakkanicko on January 28, 2014 14:33

Hi !

I have a weird issue. I have a domain class, like this :

class Test {
    Date date
    static constraints = {
    }
}

And a REST resource like this :

@Path('/api/test')
class TestResource {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    Response postTestRepresentation(Test tr) {
        Response.ok(tr).build()
    }
}

In my REST client, if I post a JSON object on /api/test like this :

{"date":"2014-01-28T15:56:37Z"}

I get this, and it's fine :

{
"class": "api.Test"
"id": null
"date": "2014-01-28T15:56:37Z"
}

But if I post a date with hours set to 12, like :

{"date":"2014-01-28T12:56:37Z"}

I get my hour digits unmarshalled to 00 and I get this :

{
"class": "api.Test"
"id": null
"date": "2014-01-28T00:56:37Z"
}

By the way, I use Grails 2.3.5 and jaxrs:0.10.

If anybody have an explanation or/and a workaround, it will be great :)

Thanks.

Copied from original issue: krasserm#42

From @davidecavestro on February 3, 2014 15:27

I guess it is due to the fact that you are passing both T and Z as constant chars within your json date(time) representation, where the literal 'Z' represents the UTC time zone.
"yyyy-MM-dd'T'hh:mm:ss'Z'" is one of the default date formats supported by grails web layer (as per http://grails.org/doc/latest/guide/theWebLayer.html Date Formats For Data Binding paragraph) but it seems not so portable.
The hour is in Hour in am/pm (1-12) format without the Am/pm marker (see SimpleDateFormat javadocs): I suspect the server uses different timezones (possibly the jvm timezone against the UTC) between the parse and the format stages, hence resulting in an unwanted change to your data.
You should consider adding

//compatible with dates expressed as "2014-01-28T12:56:37+0100"
grails.databinding.dateFormats = ["yyyy-MM-dd'T'HH:mm:ssZ"]

to your grails-app/conf/Config.groovy and adapt your grails-app/conf/Bootstrap.groovy with the same pattern as follows

import grails.converters.JSON

class BootStrap {

    def init = { servletContext ->
        JSON.registerObjectMarshaller(Date) {
            return it?.format("yyyy-MM-dd'T'HH:mm:ssZ")
         }
    }
    def destroy = {
    }
}

in order to control both the date parsing and formatting stages.

Please note that the server still continues returning dates converted to its own timezone, but now the returned data is correct (if converted to the original timezone it is the same as the orginal one).
Please let me know if you need further help or if you find a better solution (maybe Grails exposes a way to control the timezone to use when you it does string to date conversions and we simply aren't aware of its availability).

From @davidecavestro on February 19, 2014 7:37

Did you succeed in correct date transfer? May I close this ticket?

Closing due to the issue being solved.

It's work but when I get current date time it response wrong data format.
Screenshot from 2019-06-03 09-15-44

And this is what I need:
Screenshot from 2019-06-03 09-23-13