Java Regular implementation of various date formatting

Source: Internet
Author: User
Tags dateformat

Java Various date formatting
"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"

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 sec, 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 12
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 splicing Current date
Dateregformat.put ("^\\d{2}\\s*:\\s*\\d{2}$", "yyyy-mm-dd-hh-mm");//13:05 splicing Current date
Dateregformat.put ("^\\d{2}\\d+\\d{1,2}\\d+\\d{1,2}$", "Yy-mm-dd");//14.10.18 (year. month. Day)
Dateregformat.put ("^\\d{1,2}\\d+\\d{1,2}$", "yyyy-dd-mm");//30.12 (Sun. Month) Stitching current year
Dateregformat.put ("^\\d{1,2}\\d+\\d{1,2}\\d+\\d{4}$", "dd-mm-yyyy");//12.21.2013 (Sun. Month)

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 ()) {
Formatter2 = 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 the current date
Datestr = curdate + "-" + datestr;
} else if (Key.equals ("^\\d{1,2}\\d+\\d{1,2}$")) {//21.1 (day. Month) Stitching current year
Datestr = curdate.substring (0, 4) + "-" + datestr;
}
Datereplace = Datestr.replaceall ("\\d+", "-");
System.out.println (Dateregexparr[i]);
strsuccess = Formatter1.format (Formatter2.parse (datereplace));
Break
}
}
} catch (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=new string[]{
"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]));
}

}

}

Java Regular implementation of various date formatting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.