In a web application, I use twitter bootstrap; on the server side, I use spring REST.
I’m using bootstrap-datetimejs.
I use two different date formats.
$('#birthdatepicker').datetimepicker({
viewMode: 'years',
format: 'DD/MM/YYYY',
allowInputToggle: true
});
$('#expirationDateCard1, #expirationDateCard2').datetimepicker({
format: 'MM/YYYY'
});
When I post via ajax call, the response is:
"{"timestamp":1436453221365,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Can not construct instance of java.util.Date from String value '09/07/2015': not a valid representation (error: Failed to parse Date value '09/07/2015': Can not parse date "09/07/2015": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))n at [Source: java.io.PushbackInputStream@2e700ae1; line: 1, column: 83]
Are there some utilities to automatically do the conversion?
4
Answers
Reading through what the server is telling you
When the problem happened
Your request had a problem with it
The problem is coming from your Java interpreter and the detailed message is
So basically, you’re trying to interpret
"09/07/2015"
withjava.util.Date
and it’s saying it doesn’t know what that means, try formatting your date differently. It is giving you some hints about acceptable ways to format your date, too. I recommend the ISO 8601 formattingJavaScript provides a method on it’s Date instances to do this quickly too
However, please notice how this is different to what you might expect due to my timezone so you may need to consider timezones when doing this.
Your date is formatted.Server is trying to transform your date string but your date string is not matched with desired format so it is failing.
I would suggest you to send your date in one of desired format mentioned in your error.
Change date format before making ajax call so server can automatically transform that into java object.
As a few answers have pointed out, you’re sending a date string of ’09/07/2015′ to your API, and the API can’t figure out what to do with it. (I’m not surprised: the API can’t know if it’s July 9th or September 7th, since either date can be represented with that string in different parts of the world.)
You’ll need to add a function on the client side (probably) which parses the date into an unambiguous format before sending it to the server. Something like this (lots of pseudocode here):
Attempting to convert to String and formatting dates by parsing only wastes time. The best way to solve this is by using new Date ();