Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception when Parsing Dates in Java

Tags:

java

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?

like image 816
Larry Avatar asked Dec 04 '22 20:12

Larry


2 Answers

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.println once, after parsing.

If you change your diagnostics to:

System.out.println("Before parsing: [" + lastEntryDate + "]");
Date date = formatter.parse(lastEntryDate);
System.out.println("After parsing: [" + date + "]");

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 SimpleDateFormat between 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.

like image 198
Jon Skeet Avatar answered Dec 25 '22 10:12

Jon Skeet


A failure that occurs "randomly" for the same input is strongly suggestive of a threading issue. And the stacktrace tells me that you are most likely using multiple threads in this test.

I suspect the problem is that you are sharing one SimpleDateFormat object between multiple threads without properly synchronizing its use. The javadoc for SimpleDateFormat says this:

"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.".

like image 24
Stephen C Avatar answered Dec 25 '22 09:12

Stephen C