Quick revision
The short answer
SimpleDateFormat is not thread-safe: while it parses or formats, it writes intermediate values into its own Calendar and number formatter fields, so two threads using the same static instance overwrite each other's work. That produces wrong dates, and when the shared number parser is corrupted it throws NumberFormatException. The fix is to switch to java.time's DateTimeFormatter, which is immutable and safe to share, or, if the old API must stay, to create a SimpleDateFormat per call or per thread.
What to remember
In this answer, we saw that SimpleDateFormat keeps a Calendar and a DecimalFormat as working state and changes them during every parse and format, so a shared static instance lets threads corrupt each other's results, giving wrong dates and NumberFormatException under load. We fixed it with the immutable DateTimeFormatter and LocalDate, saw how to stay correct with one SimpleDateFormat per call or per thread when the old API must remain, and finished by searching the codebase for other shared java.text formatters, including fields of singleton beans.
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: while it parses or formats, it writes intermediate values into its own Calendar and number formatter fields, so two threads using the same static instance overwrite each other's work. That produces wrong dates, and when the shared number parser is corrupted it throws NumberFormatException. The fix is to switch to java.time's DateTimeFormatter, which is immutable and safe to share, or, if the old API must stay, to create a SimpleDateFormat per call or per thread.
Why this question is asked
Date formatting looks like the most harmless code in a service, which is why this bug survives code review for years. It only shows up under concurrent load, it appears and disappears, and the stack traces point at the JDK rather than at our code. Interviewers ask it because it tests whether we know that 'final' does not mean 'safe to share', and whether we can reason about hidden mutable state inside a library class. In this answer, we will recap what thread safety means here, look inside SimpleDateFormat to see the shared state, reproduce the failure, and then go through the fixes and the follow-up search for similar classes.
1A Quick Recap: Final Is Not the Same as Immutable
The field in the question is private static final. Static means there is one instance for the whole class, and so for every thread that runs this code. Final means the field always points at the same object; nobody can assign a different formatter to it. Neither word says anything about what happens inside the object.
An object is safe to share between threads if it is immutable, meaning its state never changes after construction, or if it protects its changing state with locks or other synchronization. A String is immutable, so a static final String is perfectly safe. A static final ArrayList is not, because every thread can still call add() on the same list. SimpleDateFormat belongs to the second group, and its Javadoc says so: date formats are not synchronized, and each thread should have its own instance.
2Where the Hidden State Lives
SimpleDateFormat extends DateFormat, which holds two working objects as fields: a Calendar, used to break a Date into year, month and day or to assemble one from those parts, and a NumberFormat (a DecimalFormat), used to read and write the digits of each field.
When we call format(date), the formatter first calls calendar.setTime(date), then reads the year, month and day back out of that same calendar. When we call parse(text), it clears the calendar, uses the number formatter to read each group of digits, sets the fields on the calendar one by one, and finally asks the calendar for the resulting Date. Both operations are a series of steps on shared fields, with no lock around them.
So if thread A has set the calendar to 2026-03-14 and is about to read the month, and thread B calls setTime with 1999-12-31 in between, thread A reads B's month. The result is a date that neither thread asked for. The number formatter is worse, because its internal digit buffer is also shared. If two threads append digits into it at the same time, the buffer can end up holding something like an empty string or two decimal points, and the parse fails with NumberFormatException: multiple points, or For input string, with an empty or garbled value.
6 messages in order. Play them, or step through them one at a time.
Thread B's setTime lands between Thread A's steps, so A reads B's fields.
3Reproducing the Failure
We can see the problem in a few lines. We parse the same known date from many threads and check every result.
Notice that the numbers change from run to run, and on a quiet machine a run can even show 0 and 0. That is typical of a race condition: the bug is real all the time, but it only becomes visible when two threads happen to interleave at the wrong moment. In production that moment comes with load, which is why the reports are intermittent.
4Fix 1: Use DateTimeFormatter From java.time
Since Java 8 the right tool is java.time.format.DateTimeFormatter. It is immutable: it holds only the pattern, the locale and similar settings, and every parse or format call keeps its working values in local variables of that call. Two threads using the same instance never touch each other's data, so a static final DateTimeFormatter is safe.
Notice that we also get a better type. A report date has no time and no time zone, and LocalDate says exactly that. With java.util.Date, every 'date' was really an instant, and the time zone of the server quietly decided which day it showed. When we must convert to or from Date at the edges of old code, we name the zone explicitly, as above.
If we rerun our stress test with a DateTimeFormatter and LocalDate, it prints 0 wrong and 0 errors every time, at any load.
5Fix 2: If SimpleDateFormat Must Stay
Sometimes the formatter is passed into an old library that only accepts java.text.DateFormat, so we cannot replace it yet. Then we stop sharing it. The simplest way is to create a new instance in each call. Creating one costs a few microseconds, which is negligible next to a report or a database query.
The ThreadLocal version gives each thread its own formatter, so no two threads ever share one. But there is a catch: with virtual threads, where every task can run on a brand new thread, a per-thread cache creates a new formatter for nearly every task anyway and only adds memory. Wrapping every call in synchronized also works, but it turns a formatting call into a point where all request threads queue. Per call is the safer default; DateTimeFormatter is the real fix.
6Searching for the Same Bug Elsewhere
A shared SimpleDateFormat is rarely the only one. The same team that wrote it probably shared other formatters too, so the fix is not finished until we look for the pattern across the codebase. The classes to search for are those in java.text that keep working state: SimpleDateFormat, DateFormat, DecimalFormat, NumberFormat, MessageFormat and ChoiceFormat. A search for 'static' near these names finds most cases, and static analysis tools such as SpotBugs and Sonar have rules that flag static fields of these types.
We should also check instance fields in Spring beans. A @Service is a singleton by default, so a private final DecimalFormat field in a service is shared by every request thread exactly like a static field, even though the word static never appears.
| Class | Safe to share? | What to do |
|---|---|---|
java.text.SimpleDateFormat | No | Replace with DateTimeFormatter |
java.text.DecimalFormat / NumberFormat | No | New instance per call, or per thread |
java.text.MessageFormat | No | New instance per call, or String.format |
java.time.format.DateTimeFormatter | Yes, immutable | Keep as static final |
| Field of a singleton Spring bean | Same as static | Apply the same rules |
7Follow-up questions
Why does the bug show up as NumberFormatException and not only as wrong dates?
Because SimpleDateFormat parses each field with a shared DecimalFormat, and that DecimalFormat keeps the digits it is reading in an internal buffer. When two threads parse at the same time, both append to and clear the same buffer, so one thread can end up converting an empty string or a string with two decimal points. The conversion inside then throws NumberFormatException, which surfaces through parse() even though our input text was valid.
Would making the method synchronized be an acceptable fix?
It is correct, because only one thread at a time can then use the formatter. But it serializes every caller on one lock, so under load request threads queue up behind a date formatting call, and we have traded a correctness bug for a contention problem. It is acceptable as an emergency patch, not as the final fix.
8Conclusion
In this answer, we saw that SimpleDateFormat keeps a Calendar and a DecimalFormat as working state and changes them during every parse and format, so a shared static instance lets threads corrupt each other's results, giving wrong dates and NumberFormatException under load. We fixed it with the immutable DateTimeFormatter and LocalDate, saw how to stay correct with one SimpleDateFormat per call or per thread when the old API must remain, and finished by searching the codebase for other shared java.text formatters, including fields of singleton beans.
Open with
The problem is that SimpleDateFormat is not thread-safe. The field is static final, but final only stops reassignment; the object itself has mutable working state. During format it sets its internal Calendar to the date and then reads the fields back, and during parse it clears that Calendar and uses a shared DecimalFormat to read the digits.
Then, if they want more
When several request threads use the same instance, one thread's setTime or digit buffer overwrites another's in the middle of the operation, so we get dates that belong to another request, or the number parser sees garbage and throws NumberFormatException. It is intermittent because it needs two threads to overlap at the wrong moment, which happens under load.
The fix I would make is to replace it with java.time: a static final DateTimeFormatter, which is immutable and safe to share, and LocalDate instead of Date, since a report date has no time zone.
If some legacy API forces me to keep SimpleDateFormat, I would create one per call, or one per thread with ThreadLocal.withInitial, rather than synchronizing, which would just make every request queue on one lock.
Then I would search the codebase for the same pattern, static or singleton-bean fields of DecimalFormat, NumberFormat and MessageFormat, and add a static analysis rule so it does not come back.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- 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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "Some reports show wrong dates or throw NumberFormatException intermittently. The code uses": https://scrivio-interview-library.netlify.app/java/concurrency-problems/e2347594bf/