/**
* 字符串日期转换为指定格式的日期
* @param str
* @return
* @throws ParseException
*
* @author zhaozj
*/
public static Date StringParseDate(String str,String format) throws ParseException{
String strFormat = Constants.getType(str);
SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
SimpleDateFormat sdf1 = new SimpleDateFormat(format);
if(str==null || "".equals(str)){
return null;
}else{
Date date = sdf.parse(str);
String strDate = sdf1.format(date);
return sdf1.parse(strDate) ;
}
}
//根据字符串日期,get日期格式
public static String getType(String date){
String result=null;
if(date.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d+")){
result=Constants.DATE_MODEL_1;
}else if(date.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")){
result=Constants.DATE_MODEL_2;
}else if(date.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}")){
result=Constants.DATE_MODEL_3;
}else if(date.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}")){
result=Constants.DATE_MODEL_4;
}else if(date.matches("\\d{4}-\\d{2}-\\d{2}")){
result=Constants.DATE_MODEL_5;
}else if(date.matches("\\d{4}-\\d{2}")){
result=Constants.DATE_MODEL_6;
}else if(date.matches("\\d{14}.\\d+")){
result=Constants.DATE_MODEL_7;
}else if(date.matches("\\d{14}")){
result=Constants.DATE_MODEL_8;
}else if(date.matches("\\d{12}")){
result=Constants.DATE_MODEL_9;
}else if(date.matches("\\d{10}")){
result=Constants.DATE_MODEL_10;
}else if(date.matches("\\d{8}")){
result=Constants.DATE_MODEL_11;
}else if(date.matches("\\d{6}")){
result=Constants.DATE_MODEL_12;
}else if(date.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d+")){
result=Constants.DATE_MODEL_13;
}else if(date.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}")){
result=Constants.DATE_MODEL_14;
}else if(date.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}")){
result=Constants.DATE_MODEL_15;
}else if(date.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}")){
result=Constants.DATE_MODEL_16;
}else if(date.matches("\\d{4}/\\d{2}/\\d{2}")){
result=Constants.DATE_MODEL_17;
}else if(date.matches("\\d{4}/\\d{2}")){
result=Constants.DATE_MODEL_18;
}
return result;
}