今天解决了一个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.
初始代码如下:
- private static final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
- format.format(new Date());
- 同一个format 多次调用会导致线性不安全,
可以改为:
- private static final String COMMON_DATE = "dd/MM/yyyy";
- SimpleDateFormat format = new SimpleDateFormat(COMMON_DATE);
- format.format(new Date());