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.
On Thu, Jan 27, 2011 at 10:37 AM, Dr Heinz M. Kabutz < [email protected]> wrote: > 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 Professionalhttp://www.javaspecialists.eu > Tel: +30 69 72 850 460 > Skype: kabutz > > > > On 1/27/11 9:05 AM, Jason Purcell wrote: > > Thanks. > > I just made it non-static as a quick-fix. > > On 26 January 2011 17:21, Dr Heinz M. Kabutz <[email protected]>wrote: > >> 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 Professionalhttp://www.javaspecialists.eu >> Tel: +30 69 72 850 460 >> Skype: kabutz >> >> >> >> On 1/26/11 3:44 PM, Jason Purcell wrote: >> >> 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. >> >> On 26 January 2011 15:41, Bruce Stewart <[email protected]> wrote: >> >>> 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. >>> >>> On Wed, Jan 26, 2011 at 3:36 PM, Jason Purcell <[email protected]>wrote: >>> >>>> 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. >>>> >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "CTJUG Tech" group. >>>> To post to this group, send email to [email protected] >>>> To unsubscribe from this group, send email to >>>> [email protected]<ctjug-tech%[email protected]> >>>> 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/ >>>> >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "CTJUG Tech" group. >>> To post to this group, send email to [email protected] >>> To unsubscribe from this group, send email to >>> [email protected]<ctjug-tech%[email protected]> >>> 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/ >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "CTJUG Tech" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected] >> 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/ >> >> -- >> You received this message because you are subscribed to the Google Groups >> "CTJUG Tech" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected]<ctjug-tech%[email protected]> >> 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/ >> > > -- > You received this message because you are subscribed to the Google Groups > "CTJUG Tech" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > 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/ > > -- > You received this message because you are subscribed to the Google Groups > "CTJUG Tech" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected]<ctjug-tech%[email protected]> > 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/ > -- You received this message because you are subscribed to the Google Groups "CTJUG Tech" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] 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/
