SimpleDateFormat is NOT immutable

Yes, SimpleDateFormat is not immutable as name may suggest. This came as complete surprise to me. When using from multiple threads then appeared exceptions like:

 java.lang.NumberFormatException: For input string: ""
 at java.lang.NumberFormatException.forInputString
 at java.lang.Long.parseLong(
 at java.lang.Long.parseLong(
 at java.text.DigitList.getLong(
 at java.text.DecimalFormat.parse(
 at java.text.SimpleDateFormat.subParse(
 at java.text.SimpleDateFormat.parse(
 at java.text.DateFormat.parse(

Quick check with documentation brought suspicion:

Date formats are not synchronized. It is recommended to create separate 
format instances for each thread. If multiple threads access a format 
concurrently, it must be synchronized externally.

Yes, it was enough to create per-thread SimpleDateFormat.

I think SimpleDateFormat is lousy name and also SimpleDateFormat breaks single responsibility principle. Much better would be decomposition into multiple classes:

  • SimpleDateFormat - immutable object only holding format of date
  • SimpleDateFormatter - mutable object used for formatting according to passed SimpleDateFormat
  • SimpleDataParser - mutable object used for parsing according to passed SimpleDateForma
Tags:  Java 
Last blog articles:
Why I am not afraid of nowadays technologies
DIY pokus: Destilace z kvasu
Hello Ruby: Dobrodružné programování
Edison EdBlocks: Using remote control
Edison 2.0 received!

Java Gradle Ubuntu Software-architecture children React Eclipse DisplayLink DIY AI česky personal Spring JavaScript AspectJ neuron SSH JavaFX Immutables FatJar Edison technologies procrastination pokusy lambda kvas ellipsis destilace chemie alkohol VisualVM Upstart Tomcat Selenium REST PrimeFaces Log4j Jooq Jackson JSF GIT Debug DI CSS

Last tweets: