zoukankan      html  css  js  c++  java
  • 多线程下 SimpleDateFormat

    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;
    	}
    }
    

      

  • 相关阅读:
    otter安装、使用
    windows下xampp安装rabbitmq的PHP扩展AMQP
    CentOS7下安装RabbitMQ
    CentOS7下开放端口
    CentOS7下安装Redis
    @b.windows.last.use
    Rspec基本语法
    ruby firefox23报错:waiting for evaluate.js load failed
    notepad++上配置ruby执行环境
    cucumber的hooks
  • 原文地址:https://www.cnblogs.com/binbang/p/6417128.html
Copyright © 2011-2022 走看看