Java Date Parse Exception

15 01 2012

Post describes maybe a bit niche and low-level problem, however sometimes it happens for me and for others too. The exception is:

java.lang.NumberFormatException: multiple points

and appears during date parsing with SimpleDateFormat:

DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
Date d1 = df.parse("2011-31-12");

in multi-threaded applications. The problem is that parse method is not thread-safe, although this is a bit surprising, since it is quite often used. It is described in Javadoc, under the Synchronization section:

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.

So You just have to be aware of this and care about synchronization on Your own. I wasn’t – my glassfish application threw exceptions that were hard to replicate – they appeared only when more than one user used application. Application itself is not multithreaded,  but glassfish sessions causes it to run in separate threads, I suppose (do You have a better clue?).

Simple Solution, I used is overriding DateFormat by my own Thread Safe SimpleDateFormat. It takes few seconds and looks OK!, although I am not using Spring – the problem is connected with Java itself and not caused by Spring (source):

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadSafeSimpleDateFormat extends DateFormat {
    private String pattern;

    public ThreadSafeSimpleDateFormat(String pattern) {
        this.pattern = pattern;

    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        return new SimpleDateFormat(pattern).format(date, toAppendTo, fieldPosition);

    public Date parse(String source, ParsePosition pos) {
        return new SimpleDateFormat(pattern).parse(source, pos);

then just redefine to ThreadSafeSimpleDateFormat instead of SimpleDateFormat in Your code and problem is solved.

Sources: 1 2

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!




Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: