0

I'm trying to figure out a way around Input type=date. I am aware that the input display differs in browsers, to clarify things I'm using Google Chrome for this one. So my simple form goes like this:

<input type="date" name="startingdate" class="form-control" id="startingdate"/>

And my controller goes like this:

public ModelAndView getDate(@FormParam("startingdate") String startingdate) {
        System.out.println(startingdate);
}

The format as shown in chrome is in mm/dd/yyyy which is what I want, but when I get the value from html to my controller in string format it suddenly becomes yyyy-mm-dd.

As an example when I enter 09/11/2018, it becomes 2018-11-09 in the controller and upon passing it to my database it becomes 0011-08-18. Is there any other way to fix the format before my controller gets the date value without changing the date format on my database or parsing using simpledateformat?

I am under the impression that it's simply the format's fault, but if there's something I completely missed I am open to suggestions.

Thanks!

UPDATE: The actual problem was on the backend parsing of the startingdate and it went completely under my nose. Fixed the thing by using:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    java.util.Date startingdateparsed = format.parse(startdate);
    java.sql.Date startdatingparse = new java.sql.Date(startingdateparsed.getTime());

Thank you for your time and insights guys!

  • Date fields submit the date in ISO-8601 format (so it is standard) and `2018-11-09` is precisely that. Also `@FormParam` isn't from Spring so you are probably using Jersey, so not sure what you are mixing. In Spring you would use `@RequestParam` and instead of a `String` use a `Date` or `LocalDate`, Spring will do the conversion for you. – M. Deinum Nov 05 '18 at 09:28
  • Is this behavior resulting in all browser? and did you try to make `startingdate` param as Date instead of String ? – Mohamed Nabli Nov 05 '18 at 09:30
  • Ah yes I made @RequestParam as Date and passed it to my server in String format via json. I just found out the parsing on my server side was the cause and am trying to fix it. – Shinobu Oshino Nov 05 '18 at 09:41

1 Answers1

0

There is a difference in the display format and the actual value of a date field in HTML5. (See this question as well).

So no there is no way to change the format that is send by the browser as that is strictly defined as yyyy-MM-dd. The display is based on the browsers locale.

In short change your backend to always parse yyyy-MM-dd instead of what you expect now.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Yeah I ended up using this on my backend to match it with the format from the controller SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date startingdateparsed = format.parse(startingdate); java.sql.Date startingdateparse = new java.sql.Date(startingdateparsed .getTime()); Thanks for your insights! – Shinobu Oshino Nov 05 '18 at 09:55
  • Instead of doing that just change the `String` to a `LocalDate` or `Date` in your signature and the framework will do that for you. – M. Deinum Nov 05 '18 at 09:58
  • I've tried using LocalDate for my entity from the start but it becomes a tinyblob so I didn't use it. – Shinobu Oshino Nov 05 '18 at 10:00
  • Fix your mapping in your entities then (and use a ORM mapper version that supports it). You are currently doing a lot of manual work you shouldn't be doing. – M. Deinum Nov 05 '18 at 10:03
  • Guess I should test LocalDate out before using it. It looks pretty built for these kind of things, and I wouldn't need to import SimpleDateFormat, date.util, and date.sql for a run-of-the-mill date parsing. – Shinobu Oshino Nov 05 '18 at 10:09