Sitemap

Date format issue or validation in Spring 3.4 along with Java17

3 min readApr 19, 2025
{"id":null,"adminname":"adi","password":"adi@1234","email":"adi@gmail.com","dob":"2000-12-222"}

if we observe the output is as follows

Date format is not proper if we observe , we will discuss below how to slove it

“dob”: “2000–12–222”

  • This is invalid — the month only has 31 days max, and 222 is not a valid day.
  • Jackson (the JSON parser used by Spring Boot) tries to parse it, fails silently, and likely defaults or falls back to a previous value or null.

Then somewhere in your code or DB default logic, a valid date like "2001-07-10" is assigned or persisted instead.

How to catch is Early as validation :

@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate dob;

Add validation to DTO:

@Past
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate dob;

Enable strict date parsing :

spring:
jackson:
deserialization:
fail-on-invalid-subtype: true
fail-on-unknown-properties: true

or properties file looks like below

spring.jackson.deserialization.fail-on-invalid-subtype=true
spring.jackson.deserialization.fail-on-unknown-properties=true
spring.jackson.date-format=yyyy-MM-dd

PropertyEffectfail-on-invalid-subtypeFails if JSON tries to deserialize into an invalid polymorphic typefail-on-unknown-propertiesFails if the JSON contains fields that don’t map to any property in your POJO

✅ These are useful for catching typos or invalid data structures early during deserialization.

Ensures LocalDate and Date fields are properly serialized as readable strings (like "2001-07-10"), not timestamps.

Its fixed now

Our Bean class was like :

package com.application.ecommerce.model;

import java.time.LocalDate;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Past;

@Entity
@Table(name = "admin")
public class Admin {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@SequenceGenerator(name = "admin_seq", sequenceName = "admin_seq", allocationSize = 1)
private Long id;
@Column(columnDefinition = "text")
private String adminname;
@Column(columnDefinition = "text")
private String email;
@Column(columnDefinition = "text")
private String password;
@Column(columnDefinition = "date")
@Past
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate dob;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the adminname
*/
public String getAdminname() {
return adminname;
}
/**
* @param adminname the adminname to set
*/
public void setAdminname(String adminname) {
this.adminname = adminname;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the dob
*/
public LocalDate getDob() {
return dob;
}
/**
* @param dob the dob to set
*/
public void setDob(LocalDate dob) {
this.dob = dob;
}
}

and
console of springboot was like below

2025-04-19T18:58:40.211+05:30  WARN 16372 --- [Ecommerce] [nio-3335-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "2000-12-222": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2000-12-222' could not be parsed, unparsed text found at index 10]

please find the repo here

--

--

Kiran Kumar
Kiran Kumar

Written by Kiran Kumar

Technophile with 12.5 years experience in IT industry | Java Technical Manager cum Architect

No responses yet