Package com.st.test;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.regex.Pattern;
public class Dateformatutil {@SuppressWarnings (' finally ') public static string FormatDate (String datestr) {
hashmap<string, string> Dateregformat = new hashmap<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 ");//March 12, 2014 13:5 34 seconds, 2014-03-12 12:05:34,2014/3/12 12: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-12 12:05 dateregformat.put (" ^\\d{4}\\D+\\d{2}\\D+\\d {2}\\d+\\d{2}$ "," yyyy-mm-dd-hh ");//2014-03-12 dateregformat.put (" ^\\d{4}\\d+\\d{2}\\d+\\d{2}$ "," yyyy-MM-d D ");//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 stitching the current date dateregformat.put (" ^\\d{2}\\s*:\\s*\\d{2}$ "," yyyy-mm-dd-hh-mm ");//13:05 stitching the current date dateregformat.put (" ^\\d{2}\\d+\\d{1,2}\\d+\\d{1,2}$ "," Yy-mm-dd ");//14.10.18 (year. month. Day) Dateregforma T.put ("^\\d{1,2}\\d+\\d{1,2}$", "yyyy-dd-mm"),//30.12 (day. Month) Stitching current year dateregformat.put ("^\\d{1,2}\\d+\\d{1,2}\\d+\\d
{4}$ "," dd-mm-yyyy "),//12.21.2013 (day. Month. Year) String curdate =new simpledateformat (" Yyyy-mm-dd "). Format (new Date ());
DateFormat formatter1 =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
DateFormat Formatter2;
String Datereplace;
String strsuccess= "";
try { For (String Key:dateRegFormat.keySet ()) {if Pattern.compile (key). Matcher (DATESTR). Matches ()) {F
Ormatter2 = new SimpleDateFormat (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 or 13:05 stitching current date Datestr = Curdate + "-" + datestr; else if (key.equals ("^\\d{1,2}\\d+\\d{1,2}$")) {//21.1 (day. Month) stitching the current year DATESTR = curdate.substring (0, 4) + "-"
+ datestr;
} Datereplace = Datestr.replaceall ("\\d+", "-");
System.out.println (Dateregexparr[i]);
strsuccess = Formatter1.format (Formatter2.parse (datereplace));
Break
Exception e) {System.err.println ("-----------------date format is invalid:" +datestr);
throw new Exception ("Invalid date format");
finally {return strsuccess; } public static void Main (string[] args) {string[] Datestrarray=newstring[]{"2014-03-12 12:05:34", "2014-03-12 12:05", "2014-03-12 12", "2014-03-12",
"2014-03", "2014", "20140312120534", "2014/03/12 12:05:34", "2014/3/12 12:5:34", "March 12, 2014 13:5 34 seconds", "201403121205", "1234567890", "20140312", "201403", "2000
13 33 13 13 13 "," 30.12.2013 "," 12.21.2013 "," 21.1 "," 13:05:34 "," 12:05 ",
"14.1.8", "14.10.18"}; for (int i=0;i<datestrarray.length;i++) {System.out.println (Datestrarray[i] + "------------------------------".
SUBSTRING (1,30-datestrarray[i].length ()) + formatdate (datestrarray[i));
}
}
}
The above is the entire contents of this article, I hope you can enjoy.