0

In my javascript code, i have a date of format MM/DD/YYYY. I need to pass this via request URL to spring controller. I tried passing as String but because of / cannot do it. What would be the best way to send the date(MM/DD/YYYY) to Spring controller.

js code:

var today = moment(); //it gives date as 05/26/2017

var requestURL = myContextPath + '/processDate/'+today+'/processDateForStack.form

controller:

@RequestMapping(value = "/{today}/processDateForStack", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public
    @ResponseBody

String getprocessStartedDate(@PathVariable("today") String today) throws Exception{

System.out.println("date : " + today); //should display 05/26/2017
//logic here

}

PS: Here i have used date as String type, i can use as Date type also.

I tried sending the date from js as MM-DD-YYYY from js code and in spring controller i have used @DateTimeFormat(pattern = "MM-dd-yyyy") while declaring the arguments but was throwing exception. Any suggestions would be helpful.

--EDITED--

When i pass date value as String from javascript, value of today at spring controller is 1495823051245. When i pass date value as Date from javascript to spring controller, below is the exception generated.

Error: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'; nested exception is org.springframework.core.convert.ConversionFailedException

When sent the date as Date from js to controller i have used @DateTimeFormat(pattern = "MM-dd-yyyy") while declaring the pathvariable.

user3684675
  • 381
  • 4
  • 8
  • 32
  • Can you include any errors that you're getting and the results of `System.out.println(...);` – BRogers May 26 '17 at 18:16
  • @BRogers - Error: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'; nested exception is org.springframework.core.convert.ConversionFailedException.This exception i got when i used @DateTimeFormat(pattern = "MM-dd-yyyy") in controller – user3684675 May 26 '17 at 18:18
  • Have you tried explicitly casting it to a `java.sql.Date` once you've received it? – BRogers May 26 '17 at 18:33
  • you mean when i get 1495823051245 as string value?I have not done casting. I want to know the best possible way.Any suggestions @BRogers – user3684675 May 26 '17 at 18:36
  • This should help you: https://stackoverflow.com/questions/15668329/convert-string-date-to-java-sql-date Do the conversion on the Java side before actually doing anything with the date (obviously you have to get the value first from the JS side – BRogers May 26 '17 at 18:39
  • @BRogers - no luck , as i'm getting date value as some string 1495823051245 when i pass to controller. Exception is Unparseable date: "1495823051245". I wonder there are no complete examples i found with passing date from javascript to spring controller... – user3684675 May 26 '17 at 18:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145233/discussion-between-brogers-and-user3684675). – BRogers May 26 '17 at 19:07

2 Answers2

0

While using @DateTimeFormat make sure you are using java.util.Date not java.sql.Date

@RequestMapping(value = "/{today}/processDateForStack", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getprocessStartedDate(@PathVariable("today") @DateTimeFormat(pattern = "MM-dd-yyyy") java.util.Date today) throws Exception {

}

You can also use "MM/dd/yyyy" pattern but in that case while requesting from js code you should use url encoding to send the request.

Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42
0

Change separator from / to - in your date representation. Spring using slash to identify path. Also use Date from java.util package instead of java.sql.

eg04lt3r
  • 2,467
  • 14
  • 19