0

In my Request DTO when I am trying to use pattern HHmm it's not working but HH:mm works.

public class RequestDTO {

    @DateTimeFormat(pattern = "HHmm")
    private LocalTime companyOfficeHoursStart;

}

Below is my MockMVC test:

String requestPayload = "{\"companyOfficeHoursStart\":\"1920\"}";
        RequestBuilder operation = post("/bookings").content(requestPayload)
                .contentType(MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON_VALUE);

How can we use HHmm format ?

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • Does this answer your question? [Spring DateTimeFormat Configuration for java.time](https://stackoverflow.com/questions/37871033/spring-datetimeformat-configuration-for-java-time) – Alex Ander Jul 12 '22 at 07:46

2 Answers2

1

@JsonFormat is a Jackson annotation, with Jackson being used to serialize/deserialize POJOs to/from JSON. @DateTimeFormat is a Spring annotation, which is used for setting the format with which the date is saved to the database.

For your use case, you're trying to serialize/deserialize a POJO, so you need to use @DateTimeFormat.

Please see this thread for more information:

https://stackoverflow.com/questions/37871033/spring-datetimeformat-configuration-for-java-time#:~:text=%40JsonFormat%20is%20a%20Jackson%20annotation,rendered%20in%20the%20JSP%20view.

Alex Ander
  • 1,430
  • 12
  • 19
-1

We need to use @JsonFormat when we want that fields of our DTO needs to be converted to JSON/XML using Jackson and @DateTimeFormat needs to be used when we want JSON to be converted in to field in DTO

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61