60

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?

user1755645
  • 905
  • 2
  • 13
  • 22

4 Answers4

103

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
  • 847
  • 3
  • 8
  • 22
Sudhakar
  • 4,753
  • 2
  • 34
  • 42
27

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

dannrob
  • 1,033
  • 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
10

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,051
  • 3
  • 13
  • 23
0

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

rcgeorge23
  • 3,454
  • 4
  • 27
  • 52