java正则实现各种日期格式化
java正则实现各种日期格式化
packagecom.st.test;
importjava.text.DateFormat;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.regex.Pattern;
publicclassDateFormatUtil{
@SuppressWarnings("finally")
publicstaticStringFormatDate(StringdateStr){
HashMap<String,String>dateRegFormat=newHashMap<String,String>();
dateRegFormat.put(
"^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D*$",
"yyyy-MM-dd-HH-mm-ss");//2014年3月12日13时5分34秒,2014-03-1212:05:34,2014/3/1212:5:34
dateRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{2}\\D+\\d{2}\\D+\\d{2}$",
"yyyy-MM-dd-HH-mm");//2014-03-1212:05
dateRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{2}\\D+\\d{2}$",
"yyyy-MM-dd-HH");//2014-03-1212
dateRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{2}$","yyyy-MM-dd");//2014-03-12
dateRegFormat.put("^\\d{4}\\D+\\d{2}$","yyyy-MM");//2014-03
dateRegFormat.put("^\\d{4}$","yyyy");//2014
dateRegFormat.put("^\\d{14}$","yyyyMMddHHmmss");//20140312120534
dateRegFormat.put("^\\d{12}$","yyyyMMddHHmm");//201403121205
dateRegFormat.put("^\\d{10}$","yyyyMMddHH");//2014031212
dateRegFormat.put("^\\d{8}$","yyyyMMdd");//20140312
dateRegFormat.put("^\\d{6}$","yyyyMM");//201403
dateRegFormat.put("^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$",
"yyyy-MM-dd-HH-mm-ss");//13:05:34拼接当前日期
dateRegFormat.put("^\\d{2}\\s*:\\s*\\d{2}$","yyyy-MM-dd-HH-mm");//13:05拼接当前日期
dateRegFormat.put("^\\d{2}\\D+\\d{1,2}\\D+\\d{1,2}$","yy-MM-dd");//14.10.18(年.月.日)
dateRegFormat.put("^\\d{1,2}\\D+\\d{1,2}$","yyyy-dd-MM");//30.12(日.月)拼接当前年份
dateRegFormat.put("^\\d{1,2}\\D+\\d{1,2}\\D+\\d{4}$","dd-MM-yyyy");//12.21.2013(日.月.年)
StringcurDate=newSimpleDateFormat("yyyy-MM-dd").format(newDate());
DateFormatformatter1=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
DateFormatformatter2;
StringdateReplace;
StringstrSuccess="";
try{
for(Stringkey:dateRegFormat.keySet()){
if(Pattern.compile(key).matcher(dateStr).matches()){
formatter2=newSimpleDateFormat(dateRegFormat.get(key));
if(key.equals("^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$")
||key.equals("^\\d{2}\\s*:\\s*\\d{2}$")){//13:05:34或13:05拼接当前日期
dateStr=curDate+"-"+dateStr;
}elseif(key.equals("^\\d{1,2}\\D+\\d{1,2}$")){//21.1(日.月)拼接当前年份
dateStr=curDate.substring(0,4)+"-"+dateStr;
}
dateReplace=dateStr.replaceAll("\\D+","-");
//System.out.println(dateRegExpArr[i]);
strSuccess=formatter1.format(formatter2.parse(dateReplace));
break;
}
}
}catch(Exceptione){
System.err.println("-----------------日期格式无效:"+dateStr);
thrownewException("日期格式无效");
}finally{
returnstrSuccess;
}
}
publicstaticvoidmain(String[]args){
String[]dateStrArray=newString[]{
"2014-03-1212:05:34",
"2014-03-1212:05",
"2014-03-1212",
"2014-03-12",
"2014-03",
"2014",
"20140312120534",
"2014/03/1212:05:34",
"2014/3/1212:5:34",
"2014年3月12日13时5分34秒",
"201403121205",
"1234567890",
"20140312",
"201403",
"20001333131313",
"30.12.2013",
"12.21.2013",
"21.1",
"13:05:34",
"12:05",
"14.1.8",
"14.10.18"
};
for(inti=0;i<dateStrArray.length;i++){
System.out.println(dateStrArray[i]+"------------------------------".substring(1,30-dateStrArray[i].length())+FormatDate(dateStrArray[i]));
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。