0

Here is my code:

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
....
private static Date parseIfModifiedSince(HttpServletRequest request) {
    String lastModifiedHeader = "";
    try {
        lastModifiedHeader = request.getHeader(IF_MODIFIED_SINCE);
        return !StringUtils.isEmpty(lastModifiedHeader) ? DATE_FORMAT.parse(lastModifiedHeader) : null;
    } catch (Exception ex) {
        log.warn("Error while parsing If-Modified-Since date: \"" + lastModifiedHeader+"\"", ex);
        return null;
    }
}

Sometimes I see an exception:

WARN ru.planeta.web.res.ResourcesController 2013-03-29 20:16:58,635: Error while parsing If-Modified-Since date: "Fri, 29 Mar 2013 16:16:28 GMT"
java.lang.NumberFormatException: multiple points

I can't find any problem in code. What's wrong with me?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Pavel Vyazankin
  • 1,470
  • 6
  • 18
  • 27
  • 1
    Not sure, but don't you need a `dd` to specify a 2-digit date? – asgs Mar 29 '13 at 17:19
  • 3
    http://stackoverflow.com/questions/4021151/java-dateformat-is-not-threadsafe-what-does-this-leads-to - may be this can help. Try makeing your DF ThreadLocal. – Mikhail Mar 29 '13 at 17:20
  • 2
    [SimpleDateFormat is not thread-safe](http://www.palantir.com/2007/07/simpledateformat-is-not-thread-safe/). – Lion Mar 29 '13 at 17:24

2 Answers2

1

Declaring the SimpleDateFormat as a class-level variable exposes you to that problem since it isn't thread-safe. I would recommend you to create a new SimpleDateFormat object each time you need one and only assign it to a local variable within the method.

1218985
  • 7,531
  • 2
  • 25
  • 31
0

As @Sarath mentioned, SimpleDateFormat is not thread safe. An alternate solution would be to use FastDateFormat, which is thread safe.

Aurand
  • 5,487
  • 1
  • 25
  • 35