今天遇到把字符串Mon Dec 10 15:15:14 CST 2012转为日期,这个字段串由于包含时区信息(CST),所以需要这么做:
/**
* 根据传入的格式,日期化字符串
*/
private String convertStrToDate(String dateStr, String source,String target,Locale locale) {
SimpleDateFormat format = new SimpleDateFormat(source, locale);
SimpleDateFormat formatNew = new SimpleDateFormat(target, locale);
Date date = null;
try {
date = format.parse(dateStr);
} catch (Exception e) {
logger.error("格式化日期类型的字符串出错:"+e, e);
}
return formatNew.format(date);
}
比如,调用时这么写:convertStrToDate(res, "EEE MMM dd HH:mm:ss zzz yyyy","yyyy-MM-dd HH:mm:ss", Locale.US);