Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/utils/DateGuesser.java @ 3471:e4250c6e1538 2.8.1
merged flys-backend/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:40 +0200 |
parents | 2e18e12fac11 |
children | 8195396b23ce |
comparison
equal
deleted
inserted
replaced
3468:f37e7e8907cb | 3471:e4250c6e1538 |
---|---|
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 (>=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 private DateGuesser() { | |
58 } | |
59 | |
60 public static final int calendarMonth(String month) { | |
61 return calendarMonth(Integer.parseInt(month)); | |
62 } | |
63 | |
64 public static final int calendarMonth(int month) { | |
65 return Math.max(Math.min(month-1, 11), 0); | |
66 } | |
67 | |
68 public static Date guessDate(String s) { | |
69 if (s == null || (s = s.trim()).length() == 0) { | |
70 throw new IllegalArgumentException(); | |
71 } | |
72 | |
73 Matcher m; | |
74 | |
75 m = YYYY_MM_DD.matcher(s); | |
76 | |
77 if (m.matches()) { | |
78 Calendar cal = Calendar.getInstance(); | |
79 String year = m.group(1); | |
80 String month = m.group(2); | |
81 String day = m.group(3); | |
82 cal.set( | |
83 Integer.parseInt(year), | |
84 calendarMonth(month), | |
85 Integer.parseInt(day), | |
86 12, 0, 0); | |
87 return cal.getTime(); | |
88 } | |
89 | |
90 m = DD_MM_YYYY.matcher(s); | |
91 | |
92 if (m.matches()) { | |
93 Calendar cal = Calendar.getInstance(); | |
94 String year = m.group(3); | |
95 String month = m.group(2); | |
96 String day = m.group(1); | |
97 cal.set( | |
98 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), | |
99 calendarMonth(month), | |
100 Integer.parseInt(m.group(1)), | |
101 12, 0, 0); | |
102 return cal.getTime(); | |
103 } | |
104 | |
105 m = MMM_YYYY.matcher(s); | |
106 | |
107 if (m.matches()) { | |
108 int month = guessMonth(m.group(2)); | |
109 if (month >= 0) { | |
110 Calendar cal = Calendar.getInstance(); | |
111 String year = m.group(3); | |
112 String day = m.group(1); | |
113 cal.set( | |
114 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), | |
115 month, | |
116 day.length() == 0 ? 15 : Integer.parseInt(day), | |
117 12, 0, 0); | |
118 return cal.getTime(); | |
119 } | |
120 } | |
121 | |
122 m = YYYY_MM_DDThh_mm.matcher(s); | |
123 | |
124 if (m.matches()) { | |
125 Calendar cal = Calendar.getInstance(); | |
126 String year = m.group(1); | |
127 String month = m.group(2); | |
128 String day = m.group(3); | |
129 String hour = m.group(4); | |
130 String minute = m.group(5); | |
131 cal.set( | |
132 Integer.parseInt(year), | |
133 calendarMonth(month), | |
134 Integer.parseInt(day), | |
135 Integer.parseInt(hour), | |
136 Integer.parseInt(minute), | |
137 0 | |
138 ); | |
139 return cal.getTime(); | |
140 } | |
141 | |
142 m = YYYY_MM_DDThh_mm_ss.matcher(s); | |
143 | |
144 if (m.matches()) { | |
145 Calendar cal = Calendar.getInstance(); | |
146 String year = m.group(1); | |
147 String month = m.group(2); | |
148 String day = m.group(3); | |
149 String hour = m.group(4); | |
150 String minute = m.group(5); | |
151 String second = m.group(6); | |
152 cal.set( | |
153 Integer.parseInt(year), | |
154 calendarMonth(month), | |
155 Integer.parseInt(day), | |
156 Integer.parseInt(hour), | |
157 Integer.parseInt(minute), | |
158 Integer.parseInt(second) | |
159 ); | |
160 return cal.getTime(); | |
161 } | |
162 | |
163 m = DD_MM_YYYYThh_mm.matcher(s); | |
164 | |
165 if (m.matches()) { | |
166 Calendar cal = Calendar.getInstance(); | |
167 String year = m.group(3); | |
168 String month = m.group(2); | |
169 String day = m.group(1); | |
170 String hour = m.group(4); | |
171 String minute = m.group(5); | |
172 cal.set( | |
173 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), | |
174 calendarMonth(month), | |
175 Integer.parseInt(day), | |
176 Integer.parseInt(hour), | |
177 Integer.parseInt(minute), | |
178 0 | |
179 ); | |
180 return cal.getTime(); | |
181 } | |
182 | |
183 m = DD_MM_YYYYThh_mm_ss.matcher(s); | |
184 | |
185 if (m.matches()) { | |
186 Calendar cal = Calendar.getInstance(); | |
187 String year = m.group(3); | |
188 String month = m.group(2); | |
189 String day = m.group(1); | |
190 String hour = m.group(4); | |
191 String minute = m.group(5); | |
192 String second = m.group(6); | |
193 cal.set( | |
194 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), | |
195 calendarMonth(month), | |
196 Integer.parseInt(day), | |
197 Integer.parseInt(hour), | |
198 Integer.parseInt(minute), | |
199 Integer.parseInt(second) | |
200 ); | |
201 return cal.getTime(); | |
202 } | |
203 | |
204 m = GARBAGE_YYYY.matcher(s); | |
205 | |
206 if (m.matches()) { | |
207 Calendar cal = Calendar.getInstance(); | |
208 String year = m.group(1); | |
209 cal.set( | |
210 Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), | |
211 5, // month | |
212 15, // day | |
213 12, 0, 0); | |
214 return cal.getTime(); | |
215 } | |
216 | |
217 throw new IllegalArgumentException(); | |
218 } | |
219 | |
220 public static void main(String [] args) { | |
221 for (int i = 0; i < args.length; ++i) { | |
222 System.out.println(args[i] + ": " + guessDate(args[i])); | |
223 } | |
224 } | |
225 } | |
226 // end of file |