因DateFormat类并不是线程安全的,所以在可能并发的情况下不能直接使用。
private final static ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
具体使用如下:
System.out.println(df.get().format(new Date()));
private final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");
public void test() {
DateTime d = fmt.parseDateTime("20160808");
}