4

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?

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

10 Comments

ok, I edited the original post in accordance with your suggestion ... still can’t seem to see why the error occurs?!
@Larry: That sugests the problem comes before you actually get to any of the code you've shown us. What does line 211 of writeFeedEntry look like?
Line 211 points to the formatter.parse() method... but I think the problem may be what Stephen suggested, as it’s true, my code does involve multiple threads
@Larry: And you're sharing a single SimpleDateFormat? Ick - yes, that would be a problem. Editing.
@M Platvoet: Nothing in my answer was wrong - it just wasn't the cause of the problem. I think there's a big difference between those two scenarios. It's not like I was giving bad advice - it's just that with the code provided, there was no reason to believe that threading would come into it. Stephen's guess was a really good one, but I don't think that means perfectly reasonable advice deserves a downvote.
|
8

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

1 Comment

that’s correct, I do have multiple threads accessing the same formatter, let me try modify to see what happens...
0

Would you mind to output stuff in two tiems so that we can actually always see what your trying to parse even if parsing fails:

System.out.print("["+lastEntryDate );
System.out.println("||"+formatter.parse(lastEntryDate)+"]”);`

Actually we can't see the input data that make parsing fail.

Comments

0

It's worthwhile to mention that, since Java 8, a new DateTimeFormatter class has been introduced.

The new DateTimeFormatter class is immutable and thread-safe. 

If we're working with Java 8 or later, using the new DateTimeFormatter class is recommended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.