Quick revision
The short answer
SimpleDateFormat is not thread-safe: it keeps the date being built in a mutable Calendar field, and number parsing in a mutable NumberFormat, so concurrent parse or format calls overwrite each other's state and produce wrong dates or exceptions such as NumberFormatException. The fix is a static final DateTimeFormatter, which is immutable and thread-safe. If the code must keep java.util.Date, a ThreadLocal<SimpleDateFormat> works as a stopgap, and a concurrent test proves the fix.
What to remember
In this answer, we saw that SimpleDateFormat keeps mutable working state, a Calendar and a NumberFormat, so a shared instance used by several threads mixes their calls together, returning wrong dates or throwing confusing NumberFormatExceptions. The fix is a static final DateTimeFormatter, which is immutable and thread-safe, with conversion to Date only at the boundary. A ThreadLocal<SimpleDateFormat> is an acceptable stopgap, and a concurrent test with thousands of different inputs proves the fix and protects it.
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.
SimpleDateFormat is not thread-safe: it keeps the date being built in a mutable Calendar field, and number parsing in a mutable NumberFormat, so concurrent parse or format calls overwrite each other's state and produce wrong dates or exceptions such as NumberFormatException. The fix is a static final DateTimeFormatter, which is immutable and thread-safe. If the code must keep java.util.Date, a ThreadLocal<SimpleDateFormat> works as a stopgap, and a concurrent test proves the fix.
Why this question is asked
This is one of the oldest bugs in Java, and it still turns up in services that have run quietly for years until traffic grows. It is a good interview question because the symptom, an occasional wrong date or a strange NumberFormatException with an empty string, points nowhere near the cause. In this answer, we will recap what SimpleDateFormat keeps inside it, reproduce the failure with a few threads, see exactly why it breaks, fix it with DateTimeFormatter, look at the ThreadLocal stopgap, and write a test that would have caught it.
1A Quick Recap: What SimpleDateFormat Keeps Inside
SimpleDateFormat is the formatter from the java.text package that came with java.util.Date. To avoid creating objects on every call, it keeps working state in its own fields. It inherits a Calendar field from DateFormat, which it uses as a scratch pad: parse() clears it and sets the year, month and day as it reads them, and format() loads the date into it before reading the fields back out. It also holds a NumberFormat, used to parse the digits, which has its own mutable buffers.
Being mutable is fine for one thread. The documentation has always said that date formats are not synchronized and that each thread should have its own instance. The bug appears when a developer, quite reasonably, makes one shared instance to save allocations.
2Reproducing the Failure
We can see it with a thread pool. Eight threads parse 2,000 different dates through the shared instance, then each thread formats the result with its own fresh SimpleDateFormat and compares it with the input.
Notice the variety of failures. Some calls return a plausible but wrong date, which is the dangerous case, because nothing complains. Others throw NumberFormatException with odd inputs like an empty string or 'multiple points', even though every input string was valid. The exact numbers change from run to run and machine to machine, which is typical of a race condition.
3Why Concurrent Calls Corrupt the State
Two threads calling parse() on the same instance share the same Calendar. Thread A clears it and sets year 2025 and month 3. Before it reads the result, thread B clears the same Calendar and sets month 7. Thread A then builds its Date from a mix of its own fields and B's. That is the wrong-date case.
The NumberFormatException comes from the shared NumberFormat. While parsing digits, it fills an internal buffer of characters. When two threads append to that buffer at the same time, the result is gibberish such as '.2025E4' or nothing at all, and converting it to a number fails. So the exception message talks about numbers, not dates, which sends people looking in the wrong place.
6 messages in order. Play them, or step through them one at a time.
Each call assumes it owns the scratch Calendar. With sharing, neither does.
4The Fix: A Static DateTimeFormatter
java.time's DateTimeFormatter was designed to avoid exactly this. It is immutable: every method that changes a setting, such as withZone or withLocale, returns a new formatter, and parsing keeps its working state in local objects created for each call. So one static final instance can be shared by every thread safely.
Notice that we returned a LocalDate where possible. A date such as 2025-03-14 has no time or zone, and java.util.Date forces us to invent both. The legacy method makes that choice, start of day in UTC, visible instead of hiding it in the default zone.
5The Stopgap: One Instance per Thread
Sometimes we cannot change the types yet, for example in a library whose public methods return Date. Then a ThreadLocal gives each thread its own SimpleDateFormat, created once and reused.
It works, but it is a stopgap. It keeps the old, lenient parser, which happily accepts 2025-02-30 as March 2. In applications with large thread pools or virtual threads, it creates one formatter per thread, potentially thousands. And in application servers, ThreadLocals on pooled threads can keep classes loaded after a redeploy. Synchronising on the shared instance is the other stopgap, but it turns every parse into a queue under load.
6Proving the Fix With a Concurrent Test
A single-threaded unit test cannot catch this bug, so the fix deserves a test that runs the parser from many threads, with many different inputs, and checks every result. It should start all threads at the same moment to increase contention. Run against the old code, it fails within a second; against the new code, it passes every time. A race test can never prove absence completely, but with thousands of calls it is strong evidence, and it stops someone from reintroducing a shared SimpleDateFormat later.
7Follow-up questions
Is the legacy DateFormat.getDateInstance() result any safer?
No. It returns a DateFormat, usually a SimpleDateFormat, with the same mutable state. Every java.text formatter for dates and numbers, including DecimalFormat, has the same rule: one instance per thread.
Why not just synchronize the parse method?
It is correct, and for low traffic it is fine. Under load, every parse waits for the lock, so the formatter becomes a bottleneck shared by all request threads. An immutable DateTimeFormatter has no lock and no contention, so it is both correct and faster.
What else should be checked when replacing SimpleDateFormat?
Behaviour differences. SimpleDateFormat is lenient by default and uses the default time zone and locale. DateTimeFormatter's ISO formatters are strict, so inputs like 2025-02-30 that used to be silently accepted will now be rejected, and zones and locales must be chosen explicitly. Those are improvements, but callers may depend on the old behaviour, so the change needs tests with real input samples.
8Conclusion
In this answer, we saw that SimpleDateFormat keeps mutable working state, a Calendar and a NumberFormat, so a shared instance used by several threads mixes their calls together, returning wrong dates or throwing confusing NumberFormatExceptions. The fix is a static final DateTimeFormatter, which is immutable and thread-safe, with conversion to Date only at the boundary. A ThreadLocal<SimpleDateFormat> is an acceptable stopgap, and a concurrent test with thousands of different inputs proves the fix and protects it.
Open with
SimpleDateFormat is not thread-safe. It keeps working state in fields: a Calendar that parse and format use as a scratch pad, and a NumberFormat for the digits. With one static instance shared across request threads, two parse calls interleave: one thread clears and sets the Calendar while another is still reading it, so we get dates mixed from two inputs, and the shared number parser's buffer gets corrupted, which is where the strange NumberFormatException with an empty string comes from.
Then, if they want more
It only shows under load, which is why the service seemed fine for a long time. The fix is to replace it with a static final DateTimeFormatter, for example ISO_LOCAL_DATE, which is immutable and safe to share, and ideally return LocalDate instead of Date. If public APIs still need Date, I convert at the boundary with an explicit zone.
If we can't change it yet, a ThreadLocal of SimpleDateFormat works as a stopgap, but it stays lenient and creates one instance per thread. I'd also check behaviour differences, since DateTimeFormatter is strict where SimpleDateFormat was lenient.
And I'd add a test that parses thousands of different dates from many threads and checks each result, which fails quickly on the old code.
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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "An old service uses a static SimpleDateFormat shared across threads and sometimes...": https://scrivio-interview-library.netlify.app/java/date-and-time/07d4a5da96/