Date parsing error

55 views
Skip to first unread message

Jason Purcell

unread,
Jan 26, 2011, 8:36:21 AM1/26/11
to CTJUG...@googlegroups.com
Hi there...

I am getting intermittent exceptions when parsing dates.

My formatter is declared as follows:

   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   dateFormat.setLenient(false);

These are some of the exceptions that have been thrown:

   java.text.ParseException: Unparseable date: "02/04/1980"
   java.text.ParseException: Unparseable date: "20/06/1985"
   java.text.ParseException: Unparseable date: "05/03/1990"
   java.text.ParseException: Unparseable date: "14/09/1973"
   java.text.ParseException: Unparseable date: "25/01/2011"
   java.text.ParseException: Unparseable date: "25/01/2011"
   java.text.ParseException: Unparseable date: "09/07/1965"
   java.text.ParseException: Unparseable date: "07/10/1974"
   java.text.ParseException: Unparseable date: "27/08/1966"

My unit tests pass when using the "unparseable" dates above, and in production dates parse correctly basically 99.999% of the time in the relevant piece of code.

As an example, the following dates parse correctly:

07/06/1978
14/06/1981
04/01/1988
03/10/1965
12/09/1977
30/01/1985

Has anyone come across this before?

Is there a problem with using SimpleDateFormat.setLenient() perhaps?

Regards,
Jason.


Michael Wiles

unread,
Jan 26, 2011, 8:40:13 AM1/26/11
to ctjug...@googlegroups.com
are you sharing the instance of SimpleDateFormat? It is in fact not thread safe - this might be causing your issue.



--
You received this message because you are subscribed to the Google Groups "CTJUG Tech" group.
To post to this group, send email to CTJUG...@googlegroups.com
To unsubscribe from this group, send email to CTJUG-Tech+...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CTJUG-Tech?hl=en
For Cape Town Java User Group home page see http://www.ctjug.org.za/
For jobs see http://jobs.gamatam.com/



--
see my blog:
http://analysis102.blogspot.com
http://audiblethoughts.blogspot.com
http://outsideofficehours.blogspot.com

Bruce Stewart

unread,
Jan 26, 2011, 8:41:11 AM1/26/11
to ctjug...@googlegroups.com
Hi Jason, the SimpleDateFormat class is not thread safe. If you were not aware of this you may have multiple threads accessing the the dateFormat instance, and this is exactly the sort of error that can occur.

Jason Purcell

unread,
Jan 26, 2011, 8:44:19 AM1/26/11
to ctjug...@googlegroups.com
I just read the same thing somewhere else now.

Always thought it was a good idea to re-use it, but I guess it's not.

Thanks for the info!!

I will implement the changes and see how things go.

Dr Heinz M. Kabutz

unread,
Jan 26, 2011, 10:21:03 AM1/26/11
to ctjug...@googlegroups.com
You could stick the instance in a ThreadLocal, that allows you to reuse it without worrying about concurrency.

Or you could use the Apache date parser.
Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 

Jason Purcell

unread,
Jan 27, 2011, 2:05:15 AM1/27/11
to ctjug...@googlegroups.com
Thanks.

I just made it non-static as a quick-fix.

Dr Heinz M. Kabutz

unread,
Jan 27, 2011, 3:37:14 AM1/27/11
to ctjug...@googlegroups.com
It's not static that's the problem, but multi-threaded access.  If your object is still being shared by multiple threads, you still have the bug in your code.

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 


Gary Jacobson

unread,
Jan 27, 2011, 3:39:09 AM1/27/11
to ctjug...@googlegroups.com
The simplest fix is to keep the SimpleDateFormat static, but instead of calling dateFormat.parse() directly, make a static synchronized parseDate() method to call it.

Jason Purcell

unread,
Jan 27, 2011, 3:52:16 AM1/27/11
to ctjug...@googlegroups.com
What I basically did was to make it an instance variable, and then have a static synchronized method that does the parsing.

I haven't been spending too thought on the problem, as it happens very rarely, and is not due to high load, but rather due to the client-side not handling double-clicks correctly.

But I will spend some time later today to fix it properly so that I will handle it correctly in the future.

Thanks for the help.
Reply all
Reply to author
Forward
0 new messages