回答編集履歴
1
・w・
test
CHANGED
@@ -3,3 +3,315 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
可変性を高めるならプロパティファイルで設定して
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
# 追記
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
プログラム
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```java
|
18
|
+
|
19
|
+
import java.text.SimpleDateFormat;
|
20
|
+
|
21
|
+
import java.time.LocalDate;
|
22
|
+
|
23
|
+
import java.time.format.DateTimeFormatter;
|
24
|
+
|
25
|
+
import java.util.Date;
|
26
|
+
|
27
|
+
import java.util.Locale;
|
28
|
+
|
29
|
+
import java.util.Objects;
|
30
|
+
|
31
|
+
import java.util.ResourceBundle;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
public class GengoValid {
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
ResourceBundle bundle = ResourceBundle.getBundle("calendar");
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("GGGGGy");
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
SimpleDateFormat format = new SimpleDateFormat("Gy", new Locale("ja", "JP", "JP"));
|
48
|
+
|
49
|
+
SimpleDateFormat format2 = new SimpleDateFormat("YYYY-MM-dd");
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
format.setLenient(false);
|
54
|
+
|
55
|
+
format2.setLenient(false);
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
public boolean check(String test) {
|
62
|
+
|
63
|
+
System.out.println(format.format(new Date()));
|
64
|
+
|
65
|
+
try {
|
66
|
+
|
67
|
+
test = test.toUpperCase();
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
String g = (test.charAt(0) + "").toUpperCase();
|
72
|
+
|
73
|
+
Integer y = Integer.valueOf(test.substring(1));
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
String start = bundle.getString("calendars." + g + ".start");
|
78
|
+
|
79
|
+
String end = bundle.getString("calendars." + g + ".end");
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
Objects.requireNonNull(start);
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
LocalDate max;
|
88
|
+
|
89
|
+
if (end == null || end.length() == 0) {
|
90
|
+
|
91
|
+
max = LocalDate.now();
|
92
|
+
|
93
|
+
} else {
|
94
|
+
|
95
|
+
max = LocalDate.parse(end);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
if (y == 1) {
|
102
|
+
|
103
|
+
// 元年はハードコーディング
|
104
|
+
|
105
|
+
return true;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
LocalDate inp = LocalDate.parse(format2.format(format.parse(test)));
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
System.out.println(inp);
|
118
|
+
|
119
|
+
System.out.println(max);
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
return inp.isBefore(max) || inp.equals(max);
|
124
|
+
|
125
|
+
} catch (Exception e) {
|
126
|
+
|
127
|
+
return false;
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
プロパティファイル
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
```properties
|
146
|
+
|
147
|
+
calendars.M.start=1868-01-01
|
148
|
+
|
149
|
+
calendars.M.end =1912-07-29
|
150
|
+
|
151
|
+
calendars.T.start=1912-07-30
|
152
|
+
|
153
|
+
calendars.T.end =1926-12-24
|
154
|
+
|
155
|
+
calendars.S.start=1926-12-25
|
156
|
+
|
157
|
+
calendars.S.end =1989-01-07
|
158
|
+
|
159
|
+
calendars.H.start=1989-01-08
|
160
|
+
|
161
|
+
calendars.H.end =2019-04-30
|
162
|
+
|
163
|
+
calendars.X.start=2019-05-01
|
164
|
+
|
165
|
+
calendars.X.end =
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
Test
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
import static org.junit.Assert.*;
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
import org.junit.Test;
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
public class GengoValidTest {
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
GengoValid valid = new GengoValid();
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
@Test
|
196
|
+
|
197
|
+
public void testM1() {
|
198
|
+
|
199
|
+
assertTrue(valid.check("M1"));
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
@Test
|
206
|
+
|
207
|
+
public void testM45() {
|
208
|
+
|
209
|
+
assertTrue(valid.check("M45"));
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
@Test
|
216
|
+
|
217
|
+
public void testM46() {
|
218
|
+
|
219
|
+
assertFalse(valid.check("M46"));
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
@Test
|
226
|
+
|
227
|
+
public void testT1() {
|
228
|
+
|
229
|
+
assertTrue(valid.check("T1"));
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
@Test
|
236
|
+
|
237
|
+
public void testT15() {
|
238
|
+
|
239
|
+
assertTrue(valid.check("T15"));
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
@Test
|
246
|
+
|
247
|
+
public void testT16() {
|
248
|
+
|
249
|
+
assertFalse(valid.check("T16"));
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
@Test
|
256
|
+
|
257
|
+
public void testS1() {
|
258
|
+
|
259
|
+
assertTrue(valid.check("S1"));
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
@Test
|
266
|
+
|
267
|
+
public void testS64() {
|
268
|
+
|
269
|
+
assertTrue(valid.check("S64"));
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
@Test
|
276
|
+
|
277
|
+
public void testS65() {
|
278
|
+
|
279
|
+
assertFalse(valid.check("S65"));
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
@Test
|
286
|
+
|
287
|
+
public void testH1() {
|
288
|
+
|
289
|
+
assertTrue(valid.check("H1"));
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
@Test
|
296
|
+
|
297
|
+
public void testH31() {
|
298
|
+
|
299
|
+
assertTrue(valid.check("H31"));
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
@Test
|
306
|
+
|
307
|
+
public void testH32() {
|
308
|
+
|
309
|
+
assertFalse(valid.check("H32"));
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
```
|