I am using the DateFormat class to parse a date from a String. It works fine alomost all the time but I then sometimes get
the following exception:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.text.DigitList.getLong(Unknown Source)
at java.text.DecimalFormat.parse(Unknown Source)
at java.text.SimpleDateFormat.subParse(Unknown Source)
at java.text.SimpleDateFormat.parse(Unknown Source)
at java.text.DateFormat.parse(Unknown Source)
I read in the following posting that the DateFormat class is not thread-safe:
http://forum.java.sun.com/thread.jspa?threadID=497693&messageID=2906187 In my situation I have a singleton class which is used by multiple threads at the same time. The DateFormat variable is
an instance-level variable. But the date format is not changed by any thread. It is the same for all threads. Therefore,
I am thinking that the problem described in the above forum posting isn't the actual cause of my exception??
Would appreciate other views on this because ....
I cannot reproduce the issue so it is difficult to find out exactly what is causing it. However, not sure whether or not this is just a coincidence but another problem I encountered on the same run seems to be that the
Date close = standardDateFormat.parse(accountDate);
seemed to get completely mixed up between the threads. So there could be a threading issue but I'm not sure why!!
standardDateFormat is a private instance level variable which does not change. A constant is used to set the date format.
accountDate is a local variable.
When I reran the program with the exact same data a few hours later it was inserted correctly this time. No change was made to the program in the meantime.
Many Thanks