1

I have a method what should find object from database by inserted date. I used LocalDate class, but if I try it in swagger I get a error message. I need only dates and format should be dd/MM/yyyy. Please help :)

Error message in swagger: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value '15/04/2022'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [15/04/2022]

Entity:

@Entity
@Table(name = "\"order\"")
public class Order {

    @Column(name = "delivery_date", nullable = false)
    private LocalDate deliveryDate

Dto:

@Data
public class OrderInfo implements Serializable {
    private LocalDate deliveryDate;

Method:

@GetMapping("/orders/date")
    @Operation(summary = "Find all orders by date")
    public List<OrderInfo> findAllOrderByDate(LocalDate date){
        return orderService.findAllOrdersByDate(date);
    }
Kilves
  • 11
  • 3
  • Maybe this [question](https://stackoverflow.com/questions/40274353/how-to-use-localdatetime-requestparam-in-spring-i-get-failed-to-convert-string) will help. Additionally how are getting the date? There is no annotation on it. – Chaosfire Sep 15 '22 at 05:53
  • add it inside your controller method `@RequestParam("date") @JsonFormat("dd/MM/yyyy") LocalDate date` – Abdalrhman Alkraien Mar 31 '23 at 19:40

4 Answers4

1

the best way to control of date format is by the request param by your controller

For example, if I expected this format "dd/MM/yyyy" Just I need to add this annotation before Date param @DateTimeFormat(pattern = "dd/MM/yyyy") in your controller method.

look at the following example:

    @GetMapping
    public ResponseEntity<MyObject> getSomeThing(
        @RequestParam(name = "fromDate", required = false)
        @DateTimeFormat(pattern = "dd/MM/yyyy")
        LocalDate fromDate,
        @RequestParam(name = "toDate", required = false)
        @DateTimeFormat(pattern = "dd/MM/yyyy")
        LocalDate toDate,
        @RequestParam Long id,
        @RequestParam String name
    ) {

        /// your business here
    }

If I need to change the date format for example to "yyyy-MM-dd" Like the above way just need to add it on the controller method by @DateTimeFormat(pattern = "yyyy-MM-dd")

And if we have another type of date like LocalDateTime and we need to control the date and time format, for example we expected this format "yyyy-MM-dd HH:mm:ss" so we need to add this annotation before LocalDateTime parameter in the controller method @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Look at the following example:

    @GetMapping
    public ResponseEntity<MyObject> getSomeThing(
        @RequestParam(name = "fromDate", required = false)
        @DateTimeFormat(pattern = "pattern = "yyyy-MM-dd HH:mm:ss")
        LocalDateTime fromDate,
        @DateTimeFormat(pattern = "pattern = "yyyy-MM-dd HH:mm:ss")
        LocalDateTime toDate,
        @RequestParam Long id,
        @RequestParam String name
    ) {

        /// your business here
    }

and that is it. :)

Abdalrhman Alkraien
  • 645
  • 1
  • 7
  • 22
0

Perhaps this will help. Convert String date to LocalDate:

String dateString = "07/11/1957";  // dd/MM/yyyy

java.time.format.DateTimeFormatter formatter = 
                java.time.format.DateTimeFormatter.ofPattern("dd/MM/yyyy");

java.time.LocalDate date = java.time.LocalDate.parse(dateString, formatter);

System.out.println(date); // Display LocalDate in console Window
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
0

Assuming that you're using a request param to accept the data parameter

@GetMapping("/orders/date")
@Operation(summary = "Find all orders by date")
public List<OrderInfo> findAllOrderByDate(
    @RequestParam("date") @JsonFormat("dd/MM/yyyy") LocalDate date
){
    return orderService.findAllOrdersByDate(date);
}

Please ensure that you have valid Jackson dependencies

Utsav10
  • 400
  • 2
  • 11
0

Solution was to add @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) before Localdate date parameter in my Controller class method. Formatting tips also helped. Thanks to all!

@GetMapping("/orders/date")
    @Operation(summary = "Find all orders by date")
    public List<OrderInfo> findAllOrderByDate(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date){
        return orderService.findAllOrdersByDate(date);
    }
Kilves
  • 11
  • 3