sbl*_*ndy 10 java date thread-safety
我正在寻找一个线程安全的替代品SimpleDateFormat.parseObject好旧FastDateFormat没有实现,只是抛出一个错误.有任何想法吗?我不需要任何花哨的东西,只需要线程安全性和处理这种模式的能力:"yyyy-MM-dd".
作为概括这篇文章你可以同步使用线程局部变量或乔达时间.
例如,使用ThreadLocals:
public class DateFormatTest {
private static final ThreadLocal<DateFormat> df
= new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd");
}
};
public Date convert(String source)
throws ParseException{
Date d = df.get().parse(source);
return d;
}
}
Run Code Online (Sandbox Code Playgroud)