码迷,mamicode.com
首页 > 编程语言 > 详细

多线程下 SimpleDateFormat

时间:2017-02-19 21:32:12      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:指定   数据转换   form   .text   stat   highlight   线程   exception   atp   

package com.shob.tt.single;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringUtil {

	/**
	 * SimpleDateFormat在多线程环境下容易造成数据转换及处理数据的不准确
	 * 处理方式:创建多个SimpleDateFormat
	 * @param formatPattern
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String formatPattern,String dateString) throws ParseException{
		return new SimpleDateFormat(formatPattern).parse(dateString);
	}
	
	public static String format(String formatPattern,Date date) throws ParseException{
		return new SimpleDateFormat(formatPattern).format(date);
	}
	
	private static ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>();
	/**
	 * ThreadLocal 类能使线程绑定到指定对象
	 * @param formatPattern
	 * @return
	 */
	public static SimpleDateFormat getSimpleDateFormat(String formatPattern){
		SimpleDateFormat sdf = null;
		sdf = tl.get();
		if(null == sdf){
			sdf = new SimpleDateFormat(formatPattern);
			tl.set(sdf);
		}
		return sdf;
	}
}

  

多线程下 SimpleDateFormat

标签:指定   数据转换   form   .text   stat   highlight   线程   exception   atp   

原文地址:http://www.cnblogs.com/binbang/p/6417128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!