21

I am trying to parse a string into a date in the constructor of an object, which we will call Example. Here is the code

private static final SimpleDateFormat sdf = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");

private long time;

public Example(String date) {
    try {
        this.time = sdf.parse(date).getTime();
    } catch (Exception e) {
        logger.log(Level.WARNING, "Exception while parsing date " + date, e);
    }
}

Now, I am creating these objects in a Tomcat instance (whether that makes any difference or not).

I get the following types of exceptions

Fri Jul 06 15:13:48 EDT 2012 WARNING: Exception while parsing date 2012-07-06 18:57:31
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:1297)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
    at java.text.DateFormat.parse(DateFormat.java:335)
    at ...
Fri Jul 06 15:13:48 EDT 2012 WARNING: Exception while parsing date 2012-07-06 19:00:07
java.lang.NumberFormatException: multiple points
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1082)
    at java.lang.Double.parseDouble(Double.java:510)
    at java.text.DigitList.getDouble(DigitList.java:151)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1934)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
    at java.text.DateFormat.parse(DateFormat.java:335)
    at ...
Fri Jul 06 15:13:48 EDT 2012 WARNING: Exception while parsing date 2012-07-06 19:13:21
java.lang.ArrayIndexOutOfBoundsException: -1
    at java.text.DigitList.fitsIntoLong(DigitList.java:212)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1295)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1934)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
    at java.text.DateFormat.parse(DateFormat.java:335)
    at ...
Fri Jul 06 15:48:06 EDT 2012 WARNING: Exception while parsing last check string 2012-07-06 19:08:08
java.lang.NumberFormatException: For input string: ".200172E4.200172"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    at java.lang.Double.parseDouble(Double.java:510)
    at java.text.DigitList.getDouble(DigitList.java:151)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
    at java.text.DateFormat.parse(DateFormat.java:335)
    at ...

So it fails for the dates

2012-07-06 18:57:31
2012-07-06 19:00:07
2012-07-06 19:13:21
2012-07-06 19:08:08

However, if I make a unit test I get the following values for time from these strings

1341615451000
1341615607000
1341616401000
1341616088000

So the SimpleDateFormat object does work... but not on the server? I have noticed that this issue occurs near the startup of the server, and then not later on, if that helps at all. Not really sure what to do next.

Using Tomcat 7.0 and Java 1.6 update 32.

6
  • i suspect you are passing empty strings into your Example constructor when you deploy.
    – hvgotcodes
    Commented Jul 6, 2012 at 19:40
  • @hvgotcodes I don't see how that's possible since I am logging the string that is passed into the constructor as you can see in the logs
    – skynet
    Commented Jul 6, 2012 at 19:42
  • 6
    Maybe it's a concurrency issue ? SimpleDateFormat seems not to be thread safe blog.palantir.com/2007/07/simpledateformat-is-not-thread-safe
    – cporte
    Commented Jul 6, 2012 at 20:08
  • 2
    @cporte wow, that was it! I synchronized access to the sdf object and it seems to be working fine. Leave an answer and I will accept it
    – skynet
    Commented Jul 6, 2012 at 20:21
  • 1
    Do not synchronize, but create a new SimpleDateFormat locally every time. That prevents an artificial bottleneck, paid by a bit of object overhead.
    – Joop Eggen
    Commented Jul 6, 2012 at 20:52

3 Answers 3

43

SimpleDateFormat is not thread safe, as explained https://www.palantir.com/2007/07/simpledateformat-is-not-thread-safe/ Sometimes, really strange or non-logical behaviors when using "static" objects comes from concurrency issues

To resolve such a case, use a new instance each time (It is better than synchronizing, as synchronization can raise a bottleneck issue)

2
  • I was getting the exact same error from an Apache Spark job I would submit to the cluster - had the hardest time figuring out what the problem was because the exact same job would run locally just fine on the exact same data. It turns out it was this same issue! I was using the same SimpleDateFormat in all the threads.
    – Brian
    Commented Dec 17, 2015 at 8:25
  • Link is broken, but why you're citing third parties instead of the Javadoc is a mystery.
    – user207421
    Commented Nov 10, 2016 at 22:12
5

SimpleDateFormat is not thread-safe: see the Javadoc.

3

As of Java 8+, it might be better to prefer a java.time based solution.

The classes in java.time are thread safe.

You can achieve the equivalent date extraction like that:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

...

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalDateTime date = LocalDateTime.parse(str, dockerDateFormatter);

and then query the fields you need from date.

More details in the JavaDoc for DateTimeFormatter and LocalDateTime.

Another TLDR resource with two ready-to-use examples.

2
  • 1
    Or skip the formatter and just replace the SPACE with a "T" for a standard ISO 8601 format used by default in java.time: LocalDateTime.parse( "2012-07-06 18:57:31".replace( " " , "T" ) ) Commented May 17, 2017 at 20:10
  • I really don't understand how this is possible. this fixed my issue. I don't understand how by using a formatter to parse for a date and the output to be from another string in another thread . like wtf? :) what did they do? :)))
    – Alex
    Commented Oct 10, 2023 at 15:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.