public static Date parseDate(String str, String parsePatterns[]) throws ParseException {
/* 253*/ if (str == null || parsePatterns == null) {
/* 254*/ throw new IllegalArgumentException("Date and Patterns must not be null");
}
/* 257*/ SimpleDateFormat parser = null;
/* 258*/ ParsePosition pos = new ParsePosition(0);
/* 259*/ for (int i = 0; i < parsePatterns.length; i++) {
/* 260*/ if (i == 0) {
/* 261*/ parser = new SimpleDateFormat(parsePatterns[0]);
} else {
/* 263*/ parser.applyPattern(parsePatterns[i]);
}
/* 265*/ pos.setIndex(0);
/* 266*/ Date date = parser.parse(str, pos);
/* 267*/ if (date != null && pos.getIndex() == str.length()) {
/* 268*/ return date;
}
}
/* 271*/ throw new ParseException("Unable to parse the date: " + str, -1);
}

public class DateUtil ...{
/**//**
* 格式化的样式
*/
private List<String> formatPatternList = new ArrayList<String>();

/**//**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(DateUtil.class);
private static DateUtil dateUtil = null;

public static void main(String[] args) ...{
Date date = DateUtil.getInstance().formatDate("2009.11");
System.out.println(date.toLocaleString());
}

/**//**
* 单例
*
* @return DateUtil
*/
public static synchronized DateUtil getInstance() ...{
if (dateUtil == null)
dateUtil = new DateUtil();
return dateUtil;
}

/**//**
* 格式化日期
*
* @param str
* @return Date
*/
public Date formatDate(String str) ...{
if (str == null) ...{
if (logger.isInfoEnabled()) ...{
logger.info("Date must not be null");
}
return new Date();
}
try ...{
SimpleDateFormat format = null;
ParsePosition pos = new ParsePosition(0);
for (int i = 0; i < formatPatternList.size(); i++) ...{
if (i == 0) ...{
format = new SimpleDateFormat(formatPatternList.get(0));
}
else ...{
format.applyPattern(formatPatternList.get(i));
}
pos.setIndex(0);
Date date = format.parse(str, pos);
if (date != null && pos.getIndex() == str.length()) ...{
return date;
}
}
}
catch (Exception e) ...{
logger.error("parser date occur error." + e);
}
return new Date();
}

public List<String> getFormatPatternList() ...{
return formatPatternList;
}

public void addFormatPattern(String formatPattern) ...{
formatPatternList.add(formatPattern);
}
public void addFormatPatterns(Collection formatPatterns) ...{
formatPatternList.addAll(formatPatterns);
}

public boolean removeFormatPattern(String formatPattern) ...{
return formatPatternList.remove(formatPattern);
}

public void removeAllFormatPattern() ...{
formatPatternList.clear();
}

private DateUtil() ...{
/**//**
* 初始一些日期样式
*/
formatPatternList.add("yyyyMMdd HHmmss");
formatPatternList.add("yyyyMMdd HHmm");
formatPatternList.add("yyyyMMdd HH");
formatPatternList.add("yyyyMMdd");
formatPatternList.add("yyyyMM");
formatPatternList.add("yyyy/MM/dd HH:mm:ss");
formatPatternList.add("yyyy/MM/dd HH:mm");
formatPatternList.add("yyyy/MM/dd HH");
formatPatternList.add("yyyy/MM/dd");
formatPatternList.add("yyyy/MM");
formatPatternList.add("yyyy");
// formatPatternList.add("MM/dd");
formatPatternList.add("yyyy/MM/dd HH/mm/ss");
formatPatternList.add("yyyy/MM/dd HH/mm");
formatPatternList.add("yyyy-MM-dd HH:mm:ss");
formatPatternList.add("yyyy-MM-dd HH:mm");
formatPatternList.add("yyyy-MM-dd HH");
formatPatternList.add("yyyy-MM-dd");
formatPatternList.add("yyyy-MM");
// formatPatternList.add("MM-dd");
formatPatternList.add("yyyy-MM-dd HH-mm-ss");
formatPatternList.add("yyyy-MM-dd HH-mm");
formatPatternList.add("yyyy.MM.dd HH:mm:ss");
formatPatternList.add("yyyy.MM.dd HH:mm");
formatPatternList.add("yyyy.MM.dd HH");
formatPatternList.add("yyyy.MM.dd");
formatPatternList.add("yyyy.MM");
// formatPatternList.add("MM.dd");
formatPatternList.add("yyyy.MM.dd HH.mm.ss");
formatPatternList.add("yyyy.MM.dd HH.mm");
// formatPatternList.add("HH:mm:ss");
// formatPatternList.add("HH:mm");
formatPatternList.add("yyyyMMdd HHmmss");
}
}
本文介绍了一个实用的日期处理工具类,该工具能够通过多种格式解析字符串为日期,并提供单例模式确保实例唯一性。它使用了SimpleDateFormat类进行日期格式的解析,并能够根据不同的日期格式字符串尝试解析输入的日期数据。
254

被折叠的 条评论
为什么被折叠?



