Edited in accordance with comments
For some reason, I seem to be getting this exception randomly occurring when I am trying to parse between Date and String formats in Java.
Here is a snippet of the code I’ve got (this is just for testing purposes):
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
for(...){
...
System.out.println("Before parsing: [" + lastEntryDate + "]");
Date date = formatter.parse(lastEntryDate);
System.out.println("After parsing: [" + date + "]”);
}
And the output:
Before parsing: [Sun Aug 07 22:45:30 EST 2011]
After parsing: [Sun Aug 07 22:45:30 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:431)
at java.lang.Long.parseLong(Long.java:468)
at java.text.DigitList.getLong(DigitList.java:177)
at java.text.DecimalFormat.parse(DecimalFormat.java:1298)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1936)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1312)
at java.text.DateFormat.parse(DateFormat.java:335)
at proj01.servicebus.ServiceBus.writeFeedEntry(ServiceBus.java:211)
at proj01.servicebus.ServiceBus.writeFeedEntries(ServiceBus.java:243)
at proj01.servicebus.ServiceBus.access$1(ServiceBus.java:241)
at proj01.servicebus.ServiceBus$1.call(ServiceBus.java:84)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
The problem is:
- sometimes, the parsing fails ?! (see for example, where it gives 00:00:00)
- sometimes, the exception shown is thrown ?!
- the problem seems to be random!
What is the likely cause and solution to this problem?
It looks like you’re trying to parse an empty string. Therefore the error is in the input data – unfortunately, in that case you won’t display the bad data, as you’re only calling
System.out.printlnonce, after parsing.If you change your diagnostics to:
I suspect you’ll see that you get an exception when the data is missing or completely invalid; where it’s parsing to “Sun Aug 07 00:00:00 EST 2011” I suspect you’ll find that the input data really does have that information.
EDIT: If you’re using multiple threads, you should not be sharing a
SimpleDateFormatbetween the threads. Either create one in each thread or (my preference) use Joda Time instead – it’s a much better library in general, and its formatters are thread-safe.