回答編集履歴
3
2つ目の方法のコード例を追加
test
CHANGED
@@ -155,3 +155,79 @@
|
|
155
155
|
=== OK ===
|
156
156
|
|
157
157
|
```
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
#追加2
|
162
|
+
|
163
|
+
1つ目,2つ目,3つ目...と試す形の場合, 3パターン程度であればあるいは直接並べても良いかもしれませんが, 例えば配列とループにして成功するまで回すという形に出来ます.
|
164
|
+
|
165
|
+
```java
|
166
|
+
|
167
|
+
private static DateTimeFormatter parsers[] = new DateTimeFormatter[] {
|
168
|
+
|
169
|
+
DateTimeFormatter.ofPattern("yyyy/MM/dd"),
|
170
|
+
|
171
|
+
DateTimeFormatter.ofPattern("yyyy-MM-dd"),
|
172
|
+
|
173
|
+
DateTimeFormatter.ofPattern("yyyyMMdd")
|
174
|
+
|
175
|
+
};
|
176
|
+
|
177
|
+
private static LocalDate parse(String str) throws DateTimeParseException {
|
178
|
+
|
179
|
+
DateTimeParseException ex = null;
|
180
|
+
|
181
|
+
for(DateTimeFormatter parser : parsers) {
|
182
|
+
|
183
|
+
try {
|
184
|
+
|
185
|
+
return LocalDate.parse(str, parser);
|
186
|
+
|
187
|
+
} catch(DateTimeParseException e) {
|
188
|
+
|
189
|
+
ex = e;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
throw ex;
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
```
|
200
|
+
|
201
|
+
```plain text
|
202
|
+
|
203
|
+
4567-12-03
|
204
|
+
|
205
|
+
4567-12-03
|
206
|
+
|
207
|
+
4567-12-03
|
208
|
+
|
209
|
+
Text '4567123' could not be parsed at index 6
|
210
|
+
|
211
|
+
Text '456712301' could not be parsed at index 0
|
212
|
+
|
213
|
+
Text '4567-12/30' could not be parsed at index 4
|
214
|
+
|
215
|
+
Text '4567=12=30' could not be parsed at index 4
|
216
|
+
|
217
|
+
Text '4567-12-00' could not be parsed at index 4
|
218
|
+
|
219
|
+
Text '4567-00-30' could not be parsed at index 4
|
220
|
+
|
221
|
+
Text '456-12-30' could not be parsed at index 0
|
222
|
+
|
223
|
+
Text '45678-12-30' could not be parsed at index 4
|
224
|
+
|
225
|
+
Text '' could not be parsed at index 0
|
226
|
+
|
227
|
+
Text 'A-B-C' could not be parsed at index 0
|
228
|
+
|
229
|
+
text
|
230
|
+
|
231
|
+
=== OK ===
|
232
|
+
|
233
|
+
```
|
2
追加
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
#追加
|
14
14
|
|
15
|
-
正規表現で解析するなら, このような感じでしょうか.
|
15
|
+
3つ目(?)として正規表現で解析するなら, このような感じでしょうか.
|
16
16
|
|
17
17
|
```java
|
18
18
|
|
@@ -121,3 +121,37 @@
|
|
121
121
|
}
|
122
122
|
|
123
123
|
```
|
124
|
+
|
125
|
+
```plain text
|
126
|
+
|
127
|
+
4567-12-03
|
128
|
+
|
129
|
+
4567-12-03
|
130
|
+
|
131
|
+
4567-12-03
|
132
|
+
|
133
|
+
unmatch format.
|
134
|
+
|
135
|
+
unmatch format.
|
136
|
+
|
137
|
+
unmatch format.
|
138
|
+
|
139
|
+
unmatch format.
|
140
|
+
|
141
|
+
unmatch format.
|
142
|
+
|
143
|
+
unmatch format.
|
144
|
+
|
145
|
+
unmatch format.
|
146
|
+
|
147
|
+
unmatch format.
|
148
|
+
|
149
|
+
unmatch format.
|
150
|
+
|
151
|
+
unmatch format.
|
152
|
+
|
153
|
+
null
|
154
|
+
|
155
|
+
=== OK ===
|
156
|
+
|
157
|
+
```
|
1
追加
test
CHANGED
@@ -7,3 +7,117 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
もう1つは, 1つ目で変換できなかったら2つ目の変換, 2つ目の変換で変換できなかったら3つ目の変換...と変換できるまで(もしくは変換するフォーマットが無くなるまで)変換を試すことです.
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
#追加
|
14
|
+
|
15
|
+
正規表現で解析するなら, このような感じでしょうか.
|
16
|
+
|
17
|
+
```java
|
18
|
+
|
19
|
+
import java.util.*;
|
20
|
+
|
21
|
+
import java.util.regex.*;
|
22
|
+
|
23
|
+
import java.time.*;
|
24
|
+
|
25
|
+
import java.time.format.*;
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
public class Main {
|
30
|
+
|
31
|
+
private static class TestCase {
|
32
|
+
|
33
|
+
final String str;
|
34
|
+
|
35
|
+
final boolean result;
|
36
|
+
|
37
|
+
private TestCase(String str, boolean result) { this.str = str; this.result = result; }
|
38
|
+
|
39
|
+
static TestCase True(String str) { return new TestCase(str, true); }
|
40
|
+
|
41
|
+
static TestCase False(String str) { return new TestCase(str, false); }
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
public static void main(String[] args) throws Exception {
|
46
|
+
|
47
|
+
TestCase testPattern[] = {
|
48
|
+
|
49
|
+
TestCase.True("45671203"),TestCase.True("4567/12/03"),TestCase.True("4567-12-03"),
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
TestCase.False("4567123"),TestCase.False("456712301"),
|
54
|
+
|
55
|
+
TestCase.False("4567-12/30"),TestCase.False("4567=12=30"),TestCase.False("4567-12-00"),
|
56
|
+
|
57
|
+
TestCase.False("4567-00-30"),TestCase.False("456-12-30"),TestCase.False("45678-12-30"),
|
58
|
+
|
59
|
+
TestCase.False(""),TestCase.False("A-B-C"),TestCase.False(null),
|
60
|
+
|
61
|
+
};
|
62
|
+
|
63
|
+
String totalResult = "=== OK ===";
|
64
|
+
|
65
|
+
for(TestCase test : testPattern) {
|
66
|
+
|
67
|
+
if(testParse(test.str) != test.result) totalResult = "*** ERR ***";
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
System.out.println(totalResult);
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
private static boolean testParse(String str) {
|
76
|
+
|
77
|
+
try {
|
78
|
+
|
79
|
+
LocalDate a = parse(str);
|
80
|
+
|
81
|
+
System.out.println(a);
|
82
|
+
|
83
|
+
return true;
|
84
|
+
|
85
|
+
} catch(Exception e) {
|
86
|
+
|
87
|
+
System.out.println(e.getMessage());
|
88
|
+
|
89
|
+
return false;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
private static Pattern DateParsePattern = Pattern.compile("(\d{4})([/-]?)(\d{2})\2(\d{2})");
|
96
|
+
|
97
|
+
private static LocalDate parse(String str) throws DateTimeParseException {
|
98
|
+
|
99
|
+
Matcher m = DateParsePattern.matcher(str);
|
100
|
+
|
101
|
+
if(!m.matches()) throw new DateTimeParseException("unmatch format.", str, -1);
|
102
|
+
|
103
|
+
try {
|
104
|
+
|
105
|
+
int year = Integer.parseInt(m.group(1));
|
106
|
+
|
107
|
+
Month month = Month.of(Integer.parseInt(m.group(3)));
|
108
|
+
|
109
|
+
int dayOfMonth = Integer.parseInt(m.group(4));
|
110
|
+
|
111
|
+
return LocalDate.of(year, month, dayOfMonth);
|
112
|
+
|
113
|
+
} catch(DateTimeException e) {
|
114
|
+
|
115
|
+
throw new DateTimeParseException("unmatch format.", str, -1, e);
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
```
|