Home > Back-end >  Various forms of date format standardization
Various forms of date format standardization

Time:03-31

In recent text parsing process need to extract the text within the date format and specification, commonly used format for "yyyy - MM - dd,"
Due to different writing, can't use the Date function or SimpleDateFormat function format, so their implements a method,
After test all with the date of the separator can accurately extract, now do not know whether perfect, the code is as follows:
//single digits in regular 
The static Pattern month=Pattern.com running (" (? <(d=-) \ \?=-) ");
//single-digit number day/regular
The static Pattern running day=Pattern.com (" (? <=-) \ \ d $");
Public static String formatDate (String sourceDate) {
SourceDate=sourceDate. ReplaceAll (" years../- ", "-")
ReplaceAll (" [number] $", "")
ReplaceAll (" crossed zero OOo0 [], "" 0")
Replace (" a ", "1")
Replace (" 2 ", "2")
Replace (" three ", "3")
Replace (" four ", "4")
Replace (" five ", "5")
Replace (" six ", "6")
Replace (" seven ", "7")
Replace (" eight "and" 8 ")
Replace (" nine ", "9")
Replace (" 22 ", "2");
//October 10 to 10
SourceDate=sourceDate. ReplaceAll (" (? //convert the ten dozen 1
SourceDate=sourceDate. ReplaceAll (" (? //the whole converting of ten 0
SourceDate=sourceDate. ReplaceAll (" (? <=) 10 $\ \ d ", "0");
//dozens of several ten delete
SourceDate=sourceDate. ReplaceAll (" (? <=\ \ d) 10 (?=\ \ d) ", "");
//in single digits adding 0
The Matcher Matcher=month. The Matcher (sourceDate);
If (the matcher. The find ()) {
SourceDate=sourceDate. Substring (0, the matcher. Start ()) + "0" + sourceDate substring (matcher. Start ());
}
//single-digit date adding 0
The matcher=day.to matcher (sourceDate);
If (the matcher. The find ()) {
SourceDate=sourceDate. Substring (0, the matcher. Start ()) + "0" + sourceDate substring (matcher. Start ());
}
Return sourceDate;
}
  • Related