-1

My Controller method

@GetMapping("/fetch/{one_date}/{two_date}")
    public List<CourierInfo> getData_between(@PathVariable(value = "one_date") @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") Date fromDate, @PathVariable(value = "two_date") @DateTimeFormat(pattern = "dd-MM-yyyy") Date toDate) throws Exception{
        System.out.println(fromDate);
        System.out.println(toDate);
        String date1 = "20-01-2020";
        String date2 = "24-01-2020";
        Date d1 = new SimpleDateFormat("dd-MM-yyyy").parse(date1);
        Date d2 = new SimpleDateFormat("dd-MM-yyyy").parse(date2);
        return bookRepository.getData_between(d1, d2);
    }

My url I am passing date in

http://localhost:8080/book_api/fetch/20-01-2020/24-01-2020

I am getting this error

Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '20-01-2020'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [20-01-2020]

How can I solve this error. I have tried my different things.

shim
  • 9,289
  • 12
  • 69
  • 108
Mayank S. Gupta
  • 69
  • 1
  • 11

1 Answers1

1

Try to change DateTimeFormat for

@PathVariable(value = "one_date") @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss")

to

@PathVariable(value = "one_date") @DateTimeFormat(pattern = "dd-MM-yyyy")

You are only inputing a date, but you instructed the formatter to format time as well.

Denis D.
  • 138
  • 1
  • 9