(转&修订)多线程下安全使用SimpleDateFormat
在多线程先使用静态SimpleDateFormat是不安全的,如果每次都new又太消耗资源,下面的办法不错,值得学习。
?
?
private static final String DATE_FORMAT ="yyyyMMddHHmmss"; protected static ThreadLocal<SimpleDateFormat> threadLocal =new ThreadLocal<SimpleDateFormat>() { protected synchronized SimpleDateFormat initialValue() { return new SimpleDateFormat(DATE_FORMAT); } }; public static DateFormat getDateFormat() { return threadLocal.get(); } public static Date parse(String textDate) throws ParseException { return getDateFormat().parse(textDate); }?
参考:http://www.cnblogs.com/jessiejacky/archive/2011/04/22/2024427.html
?
--end
?
?