I am trying to write a REST-API with Jersey. From javascript I get a Datestring like:
Tue Oct 16 2012 07:10:55 GMT+0200 (CEST)
(That’s what
new Date().toString()
does, but this is not in my scope.)
This date string can be parsed by implementing an own @ContextResolver. I been googling for about a day and the best SimpleDateFormat I could put together is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'z '('z')'", Locale.ROOT);
But as you can see the ‘GMT’-part and the brackets are “hard-coded”. Is there any better solution without changing the javascript part?
@kidmenot: Thanks for the link, but that solution requires to handle the javascript-date “manually” on the java side and to change the Javascript-side. So finally we needed to change the javascript-part only. There was
scattered all over the js-code. So just changing that to
solved the issue, because later on the was a
just before sending it to the server. That JSON.stringify() calls myDate.toJSON() which returns an ISO-8601 datestring which gets processed by Jersey without the need for any further coding.
So we chose this solution to spare us from future complications.
Edit half a year later:
This parse problem occured often and was finally inevitable because of some js-framework.
The following is our Java-Solution:
If you got a better solution, please post it here. Thx