Weird NumberFormatException When Using SimpleDateFormat
Contents
This post is also available in following languages
Updated at 2019-09-01:
Starting from JDK 8, the issue described in this post is no longer a problem thanks to the new Date Time API. Basically, this API is loosely based on the popular Java library JodaTime. It has advantages of Joda Time, and is very clear, concise and yet very flexible. Another advantage is that all time representations in Java 8 Date Time API are immutable and thus thread-safe. Of course, the old problematic SimpleDateFormat
is replaced by more elegant tools.
Let’s take a look at the usage before and after:
|
|
|
|
Original Post:
SimpleDateFormat
from JDK is not thread-safe, so be careful when it is used in a concurrent environment. Although it is clearly stated in the JavaDoc of the API that it’s not synchronized, I think many would not notice it until we encounter problems caused by the issue.
Let’s see an example first.
|
|
Above program starts 5 threads, each of which parses a date string using shared SimpleDateFormat
. Run above program several times, very likely you will get one of the below unexpected results or exceptions.
- Exception in thread “Thread-4” java.lang.NumberFormatException: For input string: “”
- Exception in thread “Thread-1” java.lang.NumberFormatException: multiple points
- Thu Jun 27 10:33:09 CST 2024
- Exception in thread “Thread-2” java.lang.NumberFormatException: empty String
- Fri Jun 27 10:33:09 CST 2200
- ……
As shown above, sometimes you get an exception (mostly the NumberFormatException: For input string: ""
), sometimes it just gives incorrect values.
To avoid the problem, you can create a SimpleDateFormat
each time when you parse a string. Of course, this is not efficient enough. If you want a more superior solution, you can use ThreadLocal
to make sure each thread has its own copy, you probably also want to cache the created instances. To see the details of this solution, you can refer to up Jesper’s blog post, it also provides a performance comparison of several solutions.
Author Gan Dong
LastMod 2019-09-01