63

I am new to Spring MVC - and I am trying to pass a date from my javascript as a request Param

My controller looks something like -

public @ResponseBody List<RecordDisplay> getRecords(
            @RequestParam(value="userID") Long userID,
            @RequestParam(value="fromDate") Date fromDate,
            @RequestParam(value="toDate") Date toDate) {

The question I have is how do I make the call from javascript - as in what should the URL look like

for eg. - /getRecords?userID=1&fromDate=06022013&toDate=08022013'

Do I need a way to parse the date so Spring can recognize it?

Anish B.
  • 9,111
  • 3
  • 21
  • 41
user1755645
  • 935
  • 2
  • 13
  • 22

4 Answers4

108

Use @DateTimeFormat("MMddyyyy")

public @ResponseBody List<RecordDisplay> getRecords(
@RequestParam(value="userID")  Long userID,
@RequestParam(value="fromDate")     @DateTimeFormat(pattern="MMddyyyy") Date fromDate,
@RequestParam(value="toDate")     @DateTimeFormat(pattern="MMddyyyy") Date toDate) {
DeadPassive
  • 877
  • 3
  • 8
  • 22
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
28

This is now @DateTimeFormat as well which supports some common ISO formats

dannrob
  • 1,061
  • 9
  • 10
  • 8
    Yes! Complementing this answer: It does looks much better like this: @RequestParam(value = "onDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate onDate, – imTachu Apr 27 '16 at 13:30
11

Use @DateTimeFormat(pattern="yyyy-MM-dd") where yyyy is year, MM is month and dd is date

public @ResponseBody List<Student> loadStudents(@DateTimeFormat(pattern="yyyy-MM-dd") Date birthDay) {
    ...
}
Kimchi Man
  • 1,131
  • 3
  • 13
  • 24
0

You should use your application.properties (or any other project properties) and set the environment variable spring.mvc.format.date.

rcgeorge23
  • 3,594
  • 4
  • 29
  • 54
notforward
  • 11
  • 1