66package net .atomique .ksar ;
77
88import net .atomique .ksar .xml .OSConfig ;
9+
910import org .slf4j .Logger ;
1011import org .slf4j .LoggerFactory ;
1112
1415import java .time .LocalTime ;
1516import java .time .format .DateTimeFormatter ;
1617import java .time .format .DateTimeParseException ;
17- import java .util .HashMap ;
18- import java .util .Map ;
18+ import java .util .List ;
19+ import java .util .Locale ;
1920import java .util .TreeSet ;
21+ import java .util .stream .Collectors ;
22+ import java .util .stream .Stream ;
2023
2124public abstract class AllParser {
2225
2326 private static final Logger log = LoggerFactory .getLogger (AllParser .class );
24- private static final Map <String , String > DATE_FORMAT_REGEXPS = new HashMap <String , String >() {
25- {
26- put ("^\\ d{8}$" , "yyyyMMdd" );
27- put ("^\\ d{1,2}-\\ d{1,2}-\\ d{4}$" , "dd-MM-yyyy" );
28- put ("^\\ d{4}-\\ d{1,2}-\\ d{1,2}$" , "yyyy-MM-dd" );
29- put ("^\\ d{1,2}/\\ d{1,2}/\\ d{4}$" , "MM/dd/yyyy" );
30- put ("^\\ d{4}/\\ d{1,2}/\\ d{1,2}$" , "yyyy/MM/dd" );
31- put ("^\\ d{1,2}\\ s[a-z]{3}\\ s\\ d{4}$" , "dd MMM yyyy" );
32- put ("^\\ d{1,2}\\ s[a-z]{4,}\\ s\\ d{4}$" , "dd MMMM yyyy" );
33- put ("^\\ d{1,2}-\\ d{1,2}-\\ d{2}$" , "dd-MM-yy" );
34- put ("^\\ d{1,2}/\\ d{1,2}/\\ d{2}$" , "MM/dd/yy" );
35- }
36- };
27+
28+ private static final List <DateTimeFormatter > DATE_FORMATS = Stream .of (
29+ "MM dd, yy" ,
30+ "MM-dd-yy" ,
31+ "MM/dd/yy" ,
32+ "MM-dd-yyyy" ,
33+ "MM/dd/yyyy" ,
34+ "dd-MM-yy" ,
35+ "dd.MM.yy" ,
36+ "dd/MM/yy" ,
37+ "dd.MM.yy." ,
38+ "dd-MM-yyyy" ,
39+ "dd.MM.yyyy" ,
40+ "dd/MM/yyyy" ,
41+ "dd.MM.yyyy." ,
42+ "yy. MM. dd" ,
43+ "yy-MM-dd" ,
44+ "yy.MM.dd" ,
45+ "yy/MM/dd" ,
46+ "yy年MM月dd日" ,
47+ "yy.dd.MM" ,
48+ "yyyy. MM. dd" ,
49+ "yyyy-MM-dd" ,
50+ "yyyy.MM.dd" ,
51+ "yyyy/MM/dd" ,
52+ "yyyy.MM.dd." ,
53+ "yyyy年MM月dd日" ,
54+ "yyyy.dd.MM" ,
55+ "yyyyMMdd" ,
56+ "dd MMM yyyy" ,
57+ "dd MMMM yyyy" ,
58+ "MMM dd yyyy" ,
59+ "MMMM dd yyyy"
60+ ).map (p -> DateTimeFormatter .ofPattern (p , Locale .US )).collect (Collectors .toList ());
3761
3862 public AllParser () {
3963
@@ -80,14 +104,7 @@ public boolean setDate(String s) {
80104 }
81105
82106 try {
83- DateTimeFormatter formatter ;
84- if ("Automatic Detection" .equals (dateFormat )) {
85- formatter = DateTimeFormatter .ofPattern (determineDateFormat (s ));
86-
87- } else {
88- formatter = DateTimeFormatter .ofPattern (dateFormat );
89- }
90-
107+ DateTimeFormatter formatter = getDateFormatter (s );
91108 currentDate = LocalDate .parse (s , formatter );
92109
93110 parsedate = currentDate ;
@@ -109,6 +126,20 @@ public boolean setDate(String s) {
109126 return true ;
110127 }
111128
129+ private DateTimeFormatter getDateFormatter (String s ) {
130+ if (dateFormatter != null ) {
131+ return dateFormatter ;
132+ }
133+ DateTimeFormatter format = null ;
134+ if ("Automatic Detection" .equals (dateFormat )) {
135+ format = determineDateFormat (s );
136+ } else {
137+ format = DateTimeFormatter .ofPattern (dateFormat );
138+ }
139+ dateFormatter = format ;
140+ return dateFormatter ;
141+ }
142+
112143 public String getDate () {
113144 if (sarStartDate .equals (sarEndDate )) {
114145 return sarStartDate ;
@@ -125,13 +156,21 @@ public String getCurrentStat() {
125156 return currentStat ;
126157 }
127158
128- public static String determineDateFormat (String dateString ) {
129- for (String regexp : DATE_FORMAT_REGEXPS .keySet ()) {
130- if (dateString .toLowerCase ().matches (regexp )) {
131- return DATE_FORMAT_REGEXPS .get (regexp );
159+ public static DateTimeFormatter determineDateFormat (String dateString ) {
160+ DateTimeFormatter best = null ;
161+ LocalDate bestDate = null ;
162+ for (DateTimeFormatter format : DATE_FORMATS ) {
163+ try {
164+ LocalDate nextDate = LocalDate .parse (dateString , format );
165+ if (bestDate == null || nextDate .compareTo (bestDate ) >= 0 ) {
166+ bestDate = nextDate ;
167+ best = format ;
168+ }
169+ } catch (DateTimeParseException e ) {
170+ /* ignore */
132171 }
133172 }
134- return null ; // Unknown format.
173+ return best ;
135174 }
136175
137176 protected String sarStartDate = null ;
@@ -159,4 +198,6 @@ public static String determineDateFormat(String dateString) {
159198 protected String dateFormat = "MM/dd/yy" ;
160199 protected String timeFormat = "HH:mm:ss" ;
161200 protected int timeColumn = 1 ;
201+
202+ private DateTimeFormatter dateFormatter ;
162203}
0 commit comments