您的位置:首页 > 编程语言 > Java开发

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

2013-05-20 11:19 471 查看
今天解决了一个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 plaincopyprint?

private static final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.format(new Date());

同一个format 多次调用会导致线性不安全,

可以改为:

[java]
view plaincopyprint?

private static final String COMMON_DATE = "dd/MM/yyyy";
SimpleDateFormat format = new SimpleDateFormat(COMMON_DATE);
format.format(new Date());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: