0

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?

3
  • To simply answer your question of a good JS utility for date conversion, Moment.js is pretty popular. Commented Jul 9, 2015 at 15:43
  • moment.js is actually a dependency of bootstrap-datetime.js, so it's already installed. Commented Jul 9, 2015 at 15:44
  • I don't know if it's the correct way to do it with moment, but on my dto object, i added: @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") that worked fine Commented Jul 10, 2015 at 15:19

4 Answers 4

2

Reading through what the server is telling you

"timestamp": 1436453221365,

When the problem happened

"status": 400,
"error": "Bad Request",

Your request had a problem with it

"exception": "org.springframework.http.converter.HttpMessageNotReadableException",

The problem is coming from your Java interpreter and the detailed message is

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"))
at [Source: java.io.PushbackInputStream@2e700ae1; line: 1, column: 83]"

So basically, you're trying to interpret "09/07/2015" with java.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 formatting

2015-07-09T00:00:00.000Z

JavaScript provides a method on it's Date instances to do this quickly too

new Date(2015, 7, 9).toISOString(); // "2015-08-08T23:00:00.000Z"

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This should be the accepted answer
1

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.

Comments

0

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):

$('form').on('submit', function () {
    let formValue = $('#birthdatepicker');
    // Split formValue into month, day, year with .split('/')
    // Update the submitted form parameters with:
    Date.new(year, month, day).toISOString();
    // Or some similar function on Date
});

Comments

0

Attempting to convert to String and formatting dates by parsing only wastes time. The best way to solve this is by using new Date ();

var d = $("#datetimepicker").datetimepicker('getValue');

new Date(d.getTime());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.