1.新建工具类
/** * 日期格式化工具类,使用ThreadLocal解决SimpleDateFormat非线程安全问题 */ public class DateTools { private static ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<>(); public static SimpleDateFormat getSimpleDateFormat(String datePattern) { SimpleDateFormat sdf = null; sdf = t1.get(); if(sdf == null) { sdf = new SimpleDateFormat(datePattern); t1.set(sdf); } return sdf; }
}
2.线程中使用
try { StationIaqi.setPubTime(DateTools.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pubtime)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }
完美解决!!!