comparison backend/src/main/java/org/dive4elements/river/backend/utils/DateGuesser.java @ 8187:3bb1c62ad732

Moved package org.dive4elements.river.utils to org.dive4elements.river.backend.utils.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 04 Sep 2014 15:03:25 +0200
parents backend/src/main/java/org/dive4elements/river/utils/DateGuesser.java@4c3ccf2b0304
children 5e38e2924c07
comparison
equal deleted inserted replaced
8186:a1ceacf15d3a 8187:3bb1c62ad732
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.backend.utils;
10
11 import java.util.Date;
12 import java.util.Calendar;
13
14 import java.util.regex.Pattern;
15 import java.util.regex.Matcher;
16
17 public final class DateGuesser {
18 public static final String [] MONTH = {
19 "jan", "feb", "mrz", "apr", "mai", "jun",
20 "jul", "aug", "sep", "okt", "nov", "dez"
21 };
22
23 public static final int guessMonth(String s) {
24 s = s.toLowerCase();
25 for (int i = 0; i < MONTH.length; ++i)
26 if (MONTH[i].equals(s)) {
27 return i;
28 }
29 return -1;
30 }
31
32 public static final Pattern YYYY_MM_DD =
33 Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
34
35 public static final Pattern DD_MM_YYYY =
36 Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})$");
37
38 public static final Pattern MMM_YYYY =
39 Pattern.compile("^(\\d{0,2})\\.?(\\w{3})\\.?(\\d{2,4})$");
40
41 public static final Pattern GARBAGE_YYYY =
42 Pattern.compile("^\\D*(\\d{2,4})$");
43
44 public static final Pattern YYYY_MM_DDThh_mm =
45 Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})$");
46
47 public static final Pattern YYYY_MM_DDThh_mm_ss =
48 Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})$");
49
50 public static final Pattern DD_MM_YYYYThh_mm =
51 Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2})$");
52
53 public static final Pattern DD_MM_YYYYThh_mm_ss =
54 Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2}):(\\d{2})$");
55
56 public static final Pattern DDMMYY =
57 Pattern.compile("^(\\d{2})(\\d{2})(\\d{2})$");
58
59 private DateGuesser() {
60 }
61
62 public static final int calendarMonth(String month) {
63 return calendarMonth(Integer.parseInt(month));
64 }
65
66 public static final int calendarMonth(int month) {
67 return Math.max(Math.min(month-1, 11), 0);
68 }
69
70 /**
71 * Guess date by trying all different patterns.
72 * Throws IllegalArgumentException if not able to guess.
73 * @param s The date to be guessed (e.g. 11.02.2001).
74 * @return the parsed Date.
75 */
76 public static Date guessDate(String s) {
77 if (s == null || (s = s.trim()).length() == 0) {
78 throw new IllegalArgumentException();
79 }
80
81 Matcher m;
82
83 m = YYYY_MM_DD.matcher(s);
84
85 if (m.matches()) {
86 Calendar cal = Calendar.getInstance();
87 String year = m.group(1);
88 String month = m.group(2);
89 String day = m.group(3);
90 cal.clear();
91 cal.set(
92 Integer.parseInt(year),
93 calendarMonth(month),
94 Integer.parseInt(day),
95 12, 0, 0);
96 return cal.getTime();
97 }
98
99 m = DD_MM_YYYY.matcher(s);
100
101 if (m.matches()) {
102 Calendar cal = Calendar.getInstance();
103 String year = m.group(3);
104 String month = m.group(2);
105 String day = m.group(1);
106 cal.clear();
107 cal.set(
108 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
109 calendarMonth(month),
110 Integer.parseInt(day),
111 12, 0, 0);
112 return cal.getTime();
113 }
114
115 m = MMM_YYYY.matcher(s);
116
117 if (m.matches()) {
118 int month = guessMonth(m.group(2));
119 if (month >= 0) {
120 Calendar cal = Calendar.getInstance();
121 String year = m.group(3);
122 String day = m.group(1);
123 cal.clear();
124 cal.set(
125 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
126 month,
127 day.length() == 0 ? 15 : Integer.parseInt(day),
128 12, 0, 0);
129 return cal.getTime();
130 }
131 }
132
133 m = YYYY_MM_DDThh_mm.matcher(s);
134
135 if (m.matches()) {
136 Calendar cal = Calendar.getInstance();
137 String year = m.group(1);
138 String month = m.group(2);
139 String day = m.group(3);
140 String hour = m.group(4);
141 String minute = m.group(5);
142 cal.clear();
143 cal.set(
144 Integer.parseInt(year),
145 calendarMonth(month),
146 Integer.parseInt(day),
147 Integer.parseInt(hour),
148 Integer.parseInt(minute),
149 0
150 );
151 return cal.getTime();
152 }
153
154 m = YYYY_MM_DDThh_mm_ss.matcher(s);
155
156 if (m.matches()) {
157 Calendar cal = Calendar.getInstance();
158 String year = m.group(1);
159 String month = m.group(2);
160 String day = m.group(3);
161 String hour = m.group(4);
162 String minute = m.group(5);
163 String second = m.group(6);
164 cal.clear();
165 cal.set(
166 Integer.parseInt(year),
167 calendarMonth(month),
168 Integer.parseInt(day),
169 Integer.parseInt(hour),
170 Integer.parseInt(minute),
171 Integer.parseInt(second)
172 );
173 return cal.getTime();
174 }
175
176 m = DD_MM_YYYYThh_mm.matcher(s);
177
178 if (m.matches()) {
179 Calendar cal = Calendar.getInstance();
180 String year = m.group(3);
181 String month = m.group(2);
182 String day = m.group(1);
183 String hour = m.group(4);
184 String minute = m.group(5);
185 cal.clear();
186 cal.set(
187 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
188 calendarMonth(month),
189 Integer.parseInt(day),
190 Integer.parseInt(hour),
191 Integer.parseInt(minute),
192 0
193 );
194 return cal.getTime();
195 }
196
197 m = DD_MM_YYYYThh_mm_ss.matcher(s);
198
199 if (m.matches()) {
200 Calendar cal = Calendar.getInstance();
201 String year = m.group(3);
202 String month = m.group(2);
203 String day = m.group(1);
204 String hour = m.group(4);
205 String minute = m.group(5);
206 String second = m.group(6);
207 cal.clear();
208 cal.set(
209 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
210 calendarMonth(month),
211 Integer.parseInt(day),
212 Integer.parseInt(hour),
213 Integer.parseInt(minute),
214 Integer.parseInt(second)
215 );
216 return cal.getTime();
217 }
218
219 m = DDMMYY.matcher(s);
220
221 if (m.matches()) {
222 Calendar cal = Calendar.getInstance();
223 String day = m.group(1);
224 String month = m.group(2);
225 String yearS = m.group(3);
226 int year = Integer.parseInt(yearS);
227
228 if (year <= cal.get(Calendar.YEAR) % 100) {
229 year += 2000;
230 }
231 else {
232 year += 1900;
233 }
234 cal.clear();
235 cal.set(
236 year,
237 Integer.parseInt(month), // month
238 Integer.parseInt(day), // day
239 12, 0, 0);
240 return cal.getTime();
241 }
242
243 m = GARBAGE_YYYY.matcher(s);
244
245 if (m.matches()) {
246 Calendar cal = Calendar.getInstance();
247 String year = m.group(1);
248 cal.clear();
249 cal.set(
250 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
251 5, // month
252 15, // day
253 12, 0, 0);
254 return cal.getTime();
255 }
256
257 throw new IllegalArgumentException();
258 }
259
260 public static void main(String [] args) {
261 for (int i = 0; i < args.length; ++i) {
262 System.out.println(args[i] + ": " + guessDate(args[i]));
263 }
264 }
265 }
266 // end of file

http://dive4elements.wald.intevation.org