Simple date format

I use a simple date format in my application as follows in a class:

static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); public static myFunction(final String strDate) { Date endDate = null; endDate = MyClass.sdf.parse(strDate); } 

I am using FindBugs, which gives the following error in the above code:

"As stated in the JavaDoc, DateFormats are inherently unsafe for multi-threaded use. The detector found a call to the DateFormat instance that was received through the static field. This looks suspicious.

Can anyone explain the error. I can not understand what the message says above.

Thank you for reading!

+4
source share
5 answers

The rest, who answered the thread safety question and removed the static keyword from SimpleDateFormat, are correct, although the code that you sent along with your question does not compile at all.

I think this is closer to the code you are looking for:

 public static Date parseDateStr(final String dateStr) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); return sdf.parse(dateStr); } 
+4
source

DateFormat are not thread safe. It is even described in javadoc . Since you declare it as a static variable, FindBugs knows that there is potential for use in multiple threads. Read more about issues and alternatives here .

In addition, your code should not compile since Java does not support local static variables. How to create a static local variable in Java?

+1
source

Removing a static element from a static SimpleDateFormat sdf = new SimpleDateFormat ("MM / dd / yyyy");

var is stored as one static instance covered by this method. This means that other threads accessing this method at the same time will make dateformat calls in the same instance that is not thread safe.

+1
source

as suggested by Pangea , and you can check the code below.

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Test { public static Date myFunction(final String strDate) throws ParseExceptoion { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); return sdf.parse(strDate); } public static void main(String[] args) throws ParseException { System.out.println(myFunction("05/18/1989").toString()); } } 
0
source

I had a problem where the JUnit test failed when building outside of eclipse (via DOS ANT build and test script), but it worked fine inside eclipse.

Under the test code was a class and a method from a batch job in which SimpleDateFormat was declared a private static final.

I did not like thread safety because this is a single-threaded batch job.

I also did not want to create an instance of SimpleDateFormat every time I needed it, because the process was processing cycles of thousands of rows of data.

I got around this by changing the declaration of SimpleDateFormat from a private static final to a private final transient.

Of course, this means creating an instance of the class, which may not work for all cases, but this is the solution if you create an instance of the class and do not want to create new instances every time, and thread safety is not a problem.

0
source

Source: https://habr.com/ru/post/1385278/


All Articles