String转Date工具类

其实就是几行代码的事情,不过封装一下,显得有逼格。

话不多说,贴代码

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.common.utils.StringUtils;

public class DateUtils 
{
	private static SimpleDateFormat sd = new SimpleDateFormat();
	
	private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
	 
	public static Date String2Date(String time ,String pattern)
	{
		sd.applyPattern(pattern);
		
		if(StringUtils.isNotEmpty(time))
		{
			try 
			{
				return sd.parse(time);
			} 
			catch (ParseException e) 
			{
				logger.error("convert Date error the time is " + time + "["+e.getMessage()+"]");
			}
		}
		
		return null;
	}
}

猜你喜欢

转载自blog.csdn.net/sheng_xinjun/article/details/81201430