SimpleDateFormat 是大多数程序员都喜欢试用的日期处理类,但部分小伙伴往往没注意到SimpleDateFormat是非线程安全的。
如果希望在多线程环境下使用建议使用这种方式
private static final ThreadLocal<SimpleDateFormat> DATE_FOMRAT =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH");
}
};
当然如果你使用的1.8以上的jdk,可以使用DateTimeFormatter