A NumberFormatException multiple points error from SimpleDateFormat means two threads parsed with one formatter at the same time. The formatter keeps its working state inside the instance. In a Kotlin app, that shared instance is usually a val in an object or a companion. Coroutines on Dispatchers.Default then call it from several threads at once. Each parse overwrites the other’s half-built number. The JDK page for SimpleDateFormat warns that date formats aren’t synchronized. A second cause keeps this bug alive. The repository takes an injected dispatcher. The unit test passes it a StandardTestDispatcher. That test runs every coroutine on one thread, so the race never happens there. Under light load the same code returns wrong timestamps without throwing. The fix is a DateTimeFormatter. That class is immutable and thread-safe. It’s native from API 26 and available below that through core library desugaring.
One shared formatter throws NumberFormatException multiple points
An orders screen loads 2,000 orders. Each has a createdAt string in UTC. The repository converts them to epoch millis in parallel, because the list is long.
data class Order(val id: Int, val createdAt: String)
object DateParser {
private val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).apply {
timeZone = TimeZone.getTimeZone("UTC")
}
fun parseMillis(s: String): Long = format.parse(s)!!.time
}
class OrderRepository(
private val dispatcher: CoroutineDispatcher = Dispatchers.Default,
) {
suspend fun timestamps(orders: List<Order>): List<Long> = coroutineScope {
orders.map { o -> async(dispatcher) { DateParser.parseMillis(o.createdAt) } }.awaitAll()
}
}
We compiled this with Kotlin 2.4.20 and kotlinx-coroutines 1.11.0. We ran it on OpenJDK 25 on a 12-core machine. All 50 runs of the full list crashed. We then counted every failure over 300 rounds of the same parse. Most were NumberFormatException: For input string with a garbled number. These are a few of the others we saw.
java.lang.NumberFormatException: multiple points
java.lang.NumberFormatException: empty String
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 19
java.lang.ClassCastException: class sun.util.calendar.Gregorian$Date cannot be cast
to class sun.util.calendar.JulianCalendar$Date
The multiple points trace never reaches your own code in its top frames.
at java.base/jdk.internal.math.FloatingDecimal.checkMultiplePoints at java.base/java.lang.Double.parseDouble at java.base/java.text.DigitList.getDouble at java.base/java.text.DecimalFormat.parse at java.base/java.text.SimpleDateFormat.subParse at java.base/java.text.SimpleDateFormat.parse
It fails in the JDK’s number parsing. That makes it easy to blame bad server data. The exact messages come from the OpenJDK implementation. A device can word them differently. The cause stays the same.
Root cause one is the parse state stored inside SimpleDateFormat
A SimpleDateFormat holds a Calendar and a NumberFormat as fields. The DateFormat reference lists both as protected fields of the base class. Each parse call writes the fields it reads into that shared Calendar. It also runs the digits through the shared number parser. Two threads in the same call overwrite each other’s partial results. One thread’s digits land in the other’s buffer. That’s how a date string ends up as “multiple points” or an empty number.
The Java SE 21 SimpleDateFormat reference says date formats are not synchronized. It recommends a separate instance per thread, or external synchronization. The Android reference for the same class carries the same warning. Putting the formatter in an object makes it one instance for the whole process. Every caller shares it.
Dispatchers.Default turns that sharing into a real race. Its reference says it uses as many threads as there are CPU cores, with a minimum of two. So even a low-end phone can run two parses at once. Each async above can land on a different worker thread.
A crash is at least visible. We also ran the repository 20,000 times on just two orders at a time. That’s closer to a real screen, where only a couple of parses overlap. 43 rounds crashed. 634 timestamps came back wrong with no exception at all. Those orders would show the wrong date or sort in the wrong place. Nothing would reach your crash reporter.
Root cause two is a test dispatcher that runs everything on one thread
The repository takes a dispatcher so it can be tested. That’s good practice. The test then passes the dispatcher that runTest expects.
@Test
fun parsesAllTimestamps() = runTest {
val repo = OrderRepository(StandardTestDispatcher(testScheduler))
val result = repo.timestamps(orders)
assertEquals(orders.map { expectedMillis(it) }, result)
}
This test passes every time. We ran it against the broken parser. All 2,000 timestamps were correct. The runTest reference explains why. The test runs on a single thread unless child coroutines use another dispatcher. Child coroutines don’t run in parallel with the test body. So the 2,000 async blocks run one after another. One at a time is exactly the use SimpleDateFormat supports.
Two causes stack here. The shared formatter makes the code unsafe. The single-threaded test makes it look safe. A test on real threads would only catch the race when the timing happens to line up. So fixing the test alone gives you a flaky test. The real fix is a formatter that has no shared state to race on.
An immutable DateTimeFormatter fixes it in the same object
object DateParser {
private val format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
fun parseMillis(s: String): Long =
LocalDateTime.parse(s, format).toInstant(ZoneOffset.UTC).toEpochMilli()
}
The DateTimeFormatter reference states the class is immutable and thread-safe. Parsing doesn’t write to the formatter, so one shared instance is fine. The time zone moves out of the formatter. ZoneOffset.UTC now says it explicitly at the conversion. We ran the fixed parser 50 times on the full list on Dispatchers.Default. There were no crashes and no wrong timestamps.
On Android, DateTimeFormatter was added in API level 26. If your minSdk is lower, turn on core library desugaring. The Java 8 support page lists a subset of java.time among the APIs it provides. This Gradle setup follows the sample on that page.
// app/build.gradle.kts
android {
compileOptions {
isCoreLibraryDesugaringEnabled = true
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")
}
Two other fixes also stopped the crash in our runs. A @Synchronized parse function had no failures. It also makes the parallel parse run one at a time. A ThreadLocal<SimpleDateFormat> also had no failures, at the cost of one formatter per worker thread. Both are acceptable in legacy code you can’t migrate yet. Neither is better than the immutable type. If your dates are plain ISO-8601 instants with a Z, Instant.parse needs no formatter at all.
The String.format locale bug throws the same NumberFormatException for a different reason, the device locale. The round itself is scored past the first fix. Hass covers that in finding the bug and still not passing.
How to explain it in a debugging round
The interviewer may show only the stack trace and ask what’s wrong with the date. Start from the message, then reach the second cause.
- Read the trace. It fails inside
DecimalFormatfromSimpleDateFormat.subParse. The input string itself is valid. - Name the shared state. One
SimpleDateFormatlives in anobject. It stores itsCalendarand number parser as fields. - Name the concurrency.
asynconDispatchers.Defaultruns parses on several threads at once. - Say what’s worse than the crash. Under light overlap, it returns wrong dates with no exception.
- Explain why tests pass.
runTestwith an injectedStandardTestDispatcherruns every coroutine on one thread. - Give the fix. Use
DateTimeFormatter, plus desugaring below API 26. Mention@SynchronizedorThreadLocalas fallbacks with their costs.
Fixes that look right and still race
- “Move it to
Dispatchers.IO.” TheDispatchers.IOreference says it shares threads withDefault. It allows even more of them. All 30 of our runs on IO crashed. - “Mark the field
@Volatile.” That only makes the reference visible across threads. The formatter’s internal fields still race. All 30 of our runs crashed. - “Catch the exception and return
null.” The crash goes away. The silent wrong timestamps stay. Some dates are now missing too. - “The server sent a bad date.” The same string parses correctly when the code runs on one thread.
- “The tests pass, so the parser is fine.” The test ran every coroutine on one thread. It never exercised the race.