0
private final static SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("ddMMMyy", Locale.ENGLISH);  //method 1

private static  Date getSimpleDate(String sDate) throws ParseException{
    SimpleDateFormat F = new  SimpleDateFormat("ddMMMyy", Locale.ENGLISH);
    System.out.println("pDAte:"+ F.parse(sDate).toString());
    return F.parse(sDate);
} //[method 2]


private static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() { 
    @Override
    protected SimpleDateFormat initialValue() {
        return new SimpleDateFormat("ddMMMyy", Locale.ENGLISH);// [method 3]
    }
};  //method3

private final static DateTimeFormatter DATE_FORMATTER = 
        new DateTimeFormatterBuilder().parseCaseInsensitive()
                                      .appendPattern("ddMMMyy")
                                      .toFormatter();  //[method 4]

When I parsed a Date by using method 1 I faced more exception ,

java.lang.NumberFormatException: For input at 
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

and

java.lang.NumberFormatException: For input string: ".1818E2.1818E2" 
            at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)

first my question: this value ".1818E2.1818E2" I don't set this value, but I face. Where is coming value. Although I am setting date value a way correct with correct format, I face this problem.

But other methods ( method2,method3,method4) is work. and solved the problems. This three method are alternative solution included. Second my question, What is among differences this methods.

public Date getDscDateAsDate() throws ParseException {
    if(getDscDate()!=null) {
        return SIMPLE_DATE_FORMAT.parse(getDscDate());
    }
    return null;                
}
xingbin
  • 23,890
  • 7
  • 43
  • 79
Yasin
  • 103
  • 1
  • 10

1 Answers1

3

SimpleDateFormat is not thread safe:

Why is Java's SimpleDateFormat not thread-safe?

"Java DateFormat is not threadsafe" what does this leads to?

In method 1, you are using a SimpleDateFormat instance in multiple threads, these threads might set some fields of this SimpleDateFormat simultaneously, producing invalid intermediate results, such as .1818E2.1818E2. You can check the full stack trace of this exception to get more information.

In method 2 and method 3, every thread has its own SimpleDateFormat instance. So it's ok.

In method 4, you are using DateTimeFormatter, it is thread safe.

Nathan Hughes
  • 85,411
  • 19
  • 161
  • 250
xingbin
  • 23,890
  • 7
  • 43
  • 79