skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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
    });
    
    Login or Signup to reply.
  4. 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());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search