DateTimeFormat accepting numbers in json request instead of date format

spring boot datetimeformat
how to pass date in json format
format json date to mm/dd/yyyy
json date format java timestamp iso8601
how to pass date in json object in java
iso 8601

I need to validate date using jsr annotations / spring rest

@Email(regexp = ".+@.+\\..+")
private String email;
private LocalDate dateOfBirth;

But its accepting below json request

{ "email": "","dateOfBirth": 7,}

and its parsing the date as 1970-01-07 (adding 7 days from 1970)

even below annotation is allowing numbers

 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")

How can I invalidate this request

Ended up writing my own deserializer

class LocalDateDeserializer extends StdDeserializer<LocalDate> {

protected LocalDateDeserializer(){

public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    return LocalDate.parse(jp.readValueAs(String.class));


Formatting JSON Dates in Spring Boot, Explore a number of different ways to format JSON dates in a Spring Boot We can use the @JsonFormat annotation in order to format a specific field: private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";. Adding a second key/value pair for the format does not seem like a good idea either since the date value would not be self-contained anymore - it would be meaningless without the format value. The conclusion can only be to not support different date formats in JSON! There can only be one format in order to be understood globally by different

Validate date using jsr notationspring rest.

Add dependency


Entity field like this.

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate expireDate;

Json request like this.

    "expireDate" :"2015-01-01"

JavaScript JSON Date Parsing and real Dates, In this post I show how JSON date serialization works, a few JSON has become a rather prevalent serialization format in recent years. You can represent strings, numbers, Booleans and even objects, The date constructor accepts a wide variety of inputs to construct a date, ISO 8601 amongst them. 2 DateTimeFormat accepting numbers in json request instead of date format Feb 25 '19 2 Capture MQ down event from Spring JMS Oct 16 '18 1 DateTimeFormat accepting numbers in json request instead of date format Mar 15 '19

An alternative:

Notice that LocalDateDeserializer and LocalDateTimeDeserializer support internally leniency features.

That being said, a clean solution that you can make available through a Bean:

ObjectMapper objectMapper() {

    final ObjectMapper objectMapper = new ObjectMapper();

    // make sure you added jackson-datatype-jsr310 dependency
    objectMapper.registerModule(new JavaTimeModule());

    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

    // option 1

    // option 2

    return objectMapper;

Another in-place alternative could be:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", lenient = OptBoolean.FALSE)

on the field/method itself

SimpleDateFormat, SimpleDateFormat is a concrete class for formatting and parsing dates in a either getTimeInstance , getDateInstance , or getDateTimeInstance in DateFormat . For parsing, both forms are accepted, independent of the number of pattern letters. doesn't support any week years, the calendar year ( 'y' ) is used instead. Using @JsonFormat with shape set to JsonFormat.Shape.NUMBER results in the default output for Date types — as the number of seconds since the epoch. The parameter pattern is not applicable to this case and is ignored: @JsonFormat(shape = JsonFormat.Shape.NUMBER) public Date getDateNum() { return new Date(); } The output is as shown below:

How to serialize Date and DateTime to JSON without losing , This blog post teaches you that depending on the way you serialize your Date JSON defines serialization for boolean, number and string, but not for Date format 2011-07-14 # DateTime format 2011-07-14T19:43:37+0100. Now the request will send with the current time (exact request time) as an epoch in seconds. Note: If you want to convert the epoch in seconds to the current time, add three 0s at the end so as to convert it into milliseconds.

On the nightmare that is JSON Dates. Plus, JSON.NET and ASP , And hit /api/post with this Knockout View Model and jQuery. "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. NET to use the Microsoft form instead of the 8601 standard as Thanks Scott! the choice of the dateTime format in JSON is really ugly! DateTime and DateTimeOffset support in System.Text.Json. 07/22/2019; 13 minutes to read; In this article. The System.Text.Json library parses and writes DateTime and DateTimeOffset values according to the ISO 8601:-2019 extended profile.

Solved: Error parsing JSON string to Datetime format (Powe , Search instead for Error parsing JSON string to Datetime format (Power BI) When I try to launch the connection I have a bad request with the When I want to select a variable the block don't allow me to choose the JSON variable. type isn't good I replaced it with number and now the system work. "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested." The latest version of Sugar handles the ugly date format from the default ASP.Net JSON serializer, but I really like using a standard date format, especially 8601. Hopefully all the JSON libraries

  • Could it be that dateOfBirth is part of @RequestBody object? Is the controller parameter annotated with @Valid there to trigger validation?
  • yes @Valid is there and validations are working on date format as well. Only issue is its allowing numbers
  • if i pass 7 , its adding 7 ms to the date
  • Your annotation is useless. Spring is not parsing the JSON. Your JSON parser (probably Jackson) is. See…. AFAIK, you'll need to use a custom deserializer to forbid numbers to be successfuly parsed.
  • spring 5.1.4 RELEASE
  • ofcourse this will work. But this will accept numbers as well try { "expireDate" :7 }
  • It is not accepting number.Giving error. did you try?
  • Can you make sure you tried this { "expireDate" :7 } instead of { "expireDate" :"7" }
  • I tried.only "2015-01-01" is work.Neigher 7 not "7".