解决Call to method of static java.text.DateFormat

今天解决了一个findbug的错误:

Call to method of static java.text.DateFormat in com.pbn.oss.resource.template.input.ws.service.InputTemplateWSServiceImpl.formatDate(Date)

As the JavaDoc states, DateFormats are inherently unsafe for multithreaded use. The detector has found a call to an instance of DateFormat that has been obtained via a static field. This looks suspicous.

初始代码如下:

[java] view plain copy print ?
  1. private static final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
  2. format.format(new Date());  
  1. 同一个format 多次调用会导致线性不安全,

可以改为:

[java] view plain copy print ?
  1. private static final String COMMON_DATE = "dd/MM/yyyy";  
  2. SimpleDateFormat format = new SimpleDateFormat(COMMON_DATE);  
  3. format.format(new Date());  

你可能感兴趣的:(解决Call to method of static java.text.DateFormat)