文章目录
- 1 SimpleDateFormat介绍
- 2 线程不安全的原因
- 3 错误信息
- 3.1 parse报错
- 3.1.1 异常信息
- 3.1.3 计算错误信息
- 3.2 format报错
- 4 解决方案
- 4.1 每次创建新的DateFormat
- 4.2 线程内变量ThreadLocal
- 4.2 线程安全的 DateTimeFormatter
1 SimpleDateFormat介绍
SimpleDateFormat是DateFormat类的主要子类,用于日期格式的转换。
2 线程不安全的原因
DateFormat类中,有个protected的属性calendar,用于存储设置的时间
。
/**
* The {@link Calendar} instance used for calculating the date-time fields
* and the instant of time. This field is used for both formatting and
* parsing.
*
* <p>Subclasses should initialize this field to a {@link Calendar}
* appropriate for the {@link Locale} associated with this
* <code>DateFormat</code>.
* @serial
*/
protected Calendar calendar;
翻译过来就是:
用于计算日期-时间字段的{@link Calendar}实例和时间的瞬间。此字段用于格式化和解析。
子类应该将这个字段初始化为Calendar适用于与此关联的Locale
DateFormat中提供了很多共有方法去更改此属性,如下:
public void setCalendar(Calendar newCalendar)
{
this.calendar = newCalendar;
}
public void setTimeZone(TimeZone zone)
{
calendar.setTimeZone(zone);
}
即DateFromat中存有当前将要转换的时间信息
,而不只是格式化相关的信息
。
如将DateFormat设置为一个全局变量。时间转换都使用此变量进行转换,每次转换时间,都将更改此全局对象中的calendar信息
。
多线程情况下会导致线程不安全的问题,即一个线程更改此属性后,在计算格式化或者换行成日期的过程中,其他线程对此属性进行了更改,导致计算报错,或者计算不正确。
此处说明下:
DateFormat本身设计的方式就不是多线程安全的。
我们常说SimpleDateFormat不是多线程安全的,其实指的就是DateFormat的问题。
在JDK中SimpleDateFormat是DateFormat的唯一子类
。
其他ja包中也存在DateFormat的子类
,使用时也需要注意线程安全的问题,如poi中的.ExcelStyleDateFormatter
3 错误信息
多线程环境下使用SimpleDateFormat进行日期转换,会出现两种错误信息:
(1)抛出异常
(2)计算错误
3.1 parse报错
测试多线程环境的的SimpleDateFormat的parse操作。
执行如下代码,进行多线程环境下的日期转换:
public class NumberFormatErrorTest {
public static final DateFormat DATE_FORMAT=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
for(int i=0;i<100;i++){
new Thread(() -> {
try {
Date date=DATE_FORMAT.parse("2020-04-04 12:00:00");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
}
}
3.1.1 异常信息
- 报错1
: multiple points
at (:1890)
at (:110)
at (:538)
at (:169)
at (:2056)
at (:1869)
at (:1514)
at (:364)
at $main$0(:18)
at (:745)
- 报错2
: For input string: “E.420022E”
at (:2043)
at (:110)
at (:538)
at (:169)
at (:2056)
at (:1869)
at (:1514)
at (:364)
at $main$0(:18)
at (:745)
- 报错3
: For input string: “”
at (:65)
at (:601)
at (:631)
at (:195)
at (:2051)
at (:1869)
at (:1514)
at (:364)
at $main$0(:18)
at (:745)
3.1.3 计算错误信息
按照上述代码,输出的日期,都应该为2020-04-04 12:00:00,但是多线程环境下运行,出现如下错误的计算结果:
2020-03-31 12:00:00
2020-04-04 00:00:00
2031-10-02 12:00:00
4202-04-04 12:00:00
可以看出,多线程情况下,SimpleDateFormat的计算结果,存在很大一部分几率计算错误,而且计算结果都是未知的。
3.2 format报错
待验证。。。。。。
4 解决方案
4.1 每次创建新的DateFormat
每次新线程内的操作,需要用到SimpleDateFormat时,都new 新的。
public class NumberFormatOk1Test {
public static void main(String[] args) {
for(int i=0;i<100;i++){
new Thread(() -> {
try {
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=dateFormat.parse("2020-04-04 12:00:00");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
}
}
4.2 线程内变量ThreadLocal
public class NumberFormatOk2Test {
private static final ThreadLocal<DateFormat> THREAD_LOCALE=new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public static void main(String[] args) {
for(int i=0;i<100;i++){
new Thread(() -> {
try {
Date date=THREAD_LOCALE.get().parse("2020-04-04 12:00:00");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
}catch (Exception e){
e.printStackTrace();
}finally {
THREAD_LOCALE.remove();
}
}).start();
}
}
}
4.2 线程安全的 DateTimeFormatter
JDK8中提供了线程安全的日期转换类DateTimeFormatter。