在开发的过程中我们会经常用到跟日期相关的操作,现总结一下日期相关的操作,以后日后方便直接拿出工类,调用。
public class DateUtil{
private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>();
private static final Object object = new Object();
/**
* 获取SimpleDateFormat
*
* @param pattern
* 日期格式
* @return SimpleDateFormat对象
* @throws RuntimeException
* 异常:非法日期格式
*/
private static SimpleDateFormat getDateFormat(String pattern)
throws RuntimeException {
SimpleDateFormat dateFormat = threadLocal.get();
if (dateFormat == null) {
synchronized (object) {
if (dateFormat == null) {
dateFormat = new SimpleDateFormat(pattern, Locale.US);
dateFormat.setLenient(false);
threadLocal.set(dateFormat);
}
}
}
dateFormat.applyPattern(pattern);
return dateFormat;
}
}