Quick revision
The short answer
For one field we put @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy") on a LocalDate, and Jackson reads and writes that field in the local format while everything else stays ISO-8601. We should avoid doing this in general APIs, because local formats are ambiguous and fragile; the right place is an adapter for a legacy client that cannot change. Wherever we accept it, we test that a badly formatted date returns a 400 with a clear message naming the field and the expected format.
What to remember
In this answer, we accepted dd.MM.yyyy for a single field with @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy") and saw that it affects only that JSON property, while query parameters need @DateTimeFormat. We turned parsing failures into a 400 that names the field, looked at why local formats are ambiguous and fragile compared with ISO-8601, kept the local format in an adapter for the legacy client, and tested the happy path, the wrong format and the impossible date.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For one field we put @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy") on a LocalDate, and Jackson reads and writes that field in the local format while everything else stays ISO-8601. We should avoid doing this in general APIs, because local formats are ambiguous and fragile; the right place is an adapter for a legacy client that cannot change. Wherever we accept it, we test that a badly formatted date returns a 400 with a clear message naming the field and the expected format.
Why this question is asked
Sooner or later every API meets a client that sends dates the way people write them in its country: 01.05.2024 in Germany, 05/01/2024 in the United States. Changing the global configuration for one client would break everyone else, so we need a precise, local tool, and we need to know when that tool becomes a liability. Interviewers ask this because it combines a small coding task with a design judgement. In this answer, we will accept the format for one field, see how parsing failures surface and make them useful, look at why local formats are fragile, and keep them at the edge with an adapter.
1Accepting dd.MM.yyyy for One Field
@JsonFormat is a Jackson annotation that overrides the format for a single property. With shape STRING and a pattern, the java.time module builds a DateTimeFormatter from the pattern and uses it for that field in both directions. dd is the day with two digits, MM the month with two digits (lowercase mm would be minutes, a classic mistake), and yyyy the year.
Notice that the pattern applies only to arrival. The mapper's global configuration, and every other date in the service, is untouched. Also notice that the annotation affects JSON bodies only. A date in a query parameter, such as ?arrival=01.05.2024, is converted by Spring, not Jackson, and needs @DateTimeFormat(pattern = "dd.MM.yyyy") on the controller parameter instead.
2What Happens With Bad Input
If the client sends 2024-05-01 or 1.5.24 to that field, the formatter fails, Jackson throws an InvalidFormatException (a JsonMappingException that knows the path to the field), and Spring MVC wraps it in HttpMessageNotReadableException. By default Spring answers 400, but the body says little more than that the request could not be read. The client cannot tell which field was wrong or what format was expected, so we add a handler that reads the cause.
Notice that the handler only uses the value and the path, and never echoes Jackson's full exception message, which can include internal class names. In a real service, the expected format would come from a small lookup per field or from documentation rather than a hard-coded sentence; the point is that the client learns which field failed.
3Why Local Formats Are Fragile
Look at 01.02.2024. A German reader sees 1 February; with the American order it would be 2 January. The string carries no marker that tells us which convention it follows, so a client that sends the wrong one gets a valid, wrong date instead of an error. ISO-8601, 2024-02-01, has one meaning everywhere, sorts correctly as text, and is what every JSON library and OpenAPI tool expects for a date.
There are quieter traps too. A two-digit year makes 24 mean 1924 or 2024 depending on the pattern. And a formatter's resolver style can decide what happens with an impossible date such as 31.02.2024: the default style may quietly adjust it to the last day of February instead of rejecting it. A test with an impossible date shows what our Jackson version does; @JsonFormat(lenient = OptBoolean.FALSE) asks for strict parsing (check that your version honours it for java.time types).
| Input | dd.MM.yyyy | ISO-8601 |
|---|---|---|
| 01.02.2024 / 2024-02-01 | 1 Feb, or 2 Jan to a US client | always 1 February 2024 |
| Sorting as text | wrong order (01.03 < 02.01) | correct order |
| Impossible date 31.02 | may be adjusted silently | rejected by default parsers |
| Tooling (OpenAPI format: date) | custom documentation | understood everywhere |
4Keeping the Local Format at the Edge
So we accept the local format only where we must: in a separate request type, or a separate endpoint, for the legacy client. That adapter converts to our normal internal type immediately, and the rest of the service never sees the local format. When the legacy client is retired, we delete the adapter and nothing else changes.
Notice that the main BookingRequest has no @JsonFormat at all. New clients get the standard format, documented as format: date in the OpenAPI spec, and only the adapter carries the exception.
5Testing the Parsing and the Errors
The tests cover the happy path, the wrong format and the impossible date, so the behaviour is fixed in writing rather than left to library defaults.
6Follow-up questions
Does @JsonFormat work on LocalDateTime and Instant too?
Yes for LocalDateTime and OffsetDateTime, with a pattern such as dd.MM.yyyy HH:mm. For Instant, a pattern needs a time zone, given with the timezone attribute, because an Instant has no fields like day or hour until a zone is applied.
How is this different from @DateTimeFormat?
@JsonFormat is read by Jackson and applies to JSON bodies. @DateTimeFormat is read by Spring's conversion service and applies to request parameters, path variables and form fields. A field that arrives in both ways needs both, which is another sign that one consistent format is simpler.
Could I configure the local format globally instead?
We could, with spring.jackson.date-format or a custom module, but then every date in every endpoint uses it and every other client breaks. A global change is only right when the whole API is designed around that format, which is rarely the case for a public API.
7Conclusion
In this answer, we accepted dd.MM.yyyy for a single field with @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy") and saw that it affects only that JSON property, while query parameters need @DateTimeFormat. We turned parsing failures into a 400 that names the field, looked at why local formats are ambiguous and fragile compared with ISO-8601, kept the local format in an adapter for the legacy client, and tested the happy path, the wrong format and the impossible date.
Open with
For a single field, I'd use @JsonFormat with shape STRING and pattern dd.MM.yyyy on a LocalDate. Jackson's java.time support then reads and writes just that property in the local format, and every other date stays ISO.
Then, if they want more
I'd point out two details: MM is month while mm is minutes, and @JsonFormat only applies to JSON bodies; for a query parameter I'd need @DateTimeFormat instead. Bad input makes Jackson throw InvalidFormatException, which Spring wraps in HttpMessageNotReadableException and answers with a 400, but the default body doesn't say which field failed.
So I'd add a handler that reads the path and value from the cause and returns a problem detail naming the field and the expected format, without echoing internals.
Then the judgement part: I'd avoid local formats in a general API. 01.02.2024 means 1 February to one client and 2 January to another, it doesn't sort as text, and impossible dates like 31.02 may be adjusted silently depending on the resolver style. ISO-8601, 2024-02-01, has one meaning and every tool understands it.
So I'd accept the local format only in an adapter, a separate request type or legacy endpoint, that converts to the normal internal request immediately. And I'd test it: the local format works, the ISO format on that field gives a 400 naming the field, and an impossible date is rejected.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A client sends dates as dd.MM.yyyy. Show how to accept this for one field and when you...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/00e2fb55a4/