I would need some clarification regarding the difference between Spring MVC formatters and converters.
My understanding of the main difference between them is that the formatter works on data that is going to be displayed to the end user such as a date, SSN or credit card number whereas the converter works on data hidden behind form controls such as the value attribute of an select's option.
Am I right or wrong? Can someone please provide advice and/or samples in order to better explain the difference between the two.
Converters are used to convert one Java type to another Java type. For example, from Long
to java.util.Date
or from Integer
to Color
or from String
to Date
. It can be used in the web tier or any other tier that needs conversion service.
Formatters are used to convert String
to another Java type and back. So, one type must be String
. You cannot, for example, write a formatter that converts a Long
to a Date
. Examples of formatters are DateFormatter
, for parsing String
to java.util.Date
and formatting a Date
. In addition, formatters' messages can be localized.
Conclusion: formatters are suitable in the web environment, such as a Spring MVC application.