How to add custom error message in org.springframework.format.annotation.DateTimeFormat?

You will have to use MessageSource bean and messages.properties file and there you can add key-value like below.

typeMismatch=Please use MM/dd/yyyy format

If you are using SpringBoot then MessageSource bean will be available by default and you only need to add key-value in messages.properties. For normal Spring application you can use ReloadableResourceBundleMessageSource or ResourceBundleMessageSource.

Property key is resolved in following order.

typeMismatch.[form].[property] 
typeMismatch.[form]
typeMismatch

Please refer http://jtuts.com/2014/11/09/validating-dates-in-spring-form-objects/


The correct way to add custom error message is through DefaultMessageCodeResolver You can use the resolver to bind errors at object + field level(typeMismatch.YourClassName.birthday = custom message.) as mentioned in the javadoc.

Find a detailed usage here