comparison flys-backend/src/main/java/org/dive4elements/river/utils/DateGuesser.java @ 5828:dfb26b03b179

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

http://dive4elements.wald.intevation.org