Я использую LocalDateTime в теле запроса моего API в Spring.
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-mm-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createdAt;
Когда в запросе указана недопустимая дата, например «2020-02-31 00:00:00», она автоматически преобразуется в «2020-02-29 00:00:00». Я хочу выбросить исключение в случае неверной даты. В официальной документации упоминается, что она конвертируется в предыдущую действительную дату.
In some cases, changing the specified field can cause the resulting date-time to become invalid,
such as changing the month from 31st January to February would make the day-of-month invalid.
In cases like this, the field is responsible for resolving the date.
Typically it will choose the previous valid date,
which would be the last valid day of February in this example.