1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
package org.asyrinx.brownie.core.util.jp;
|
6
|
|
|
7
|
|
import java.text.DateFormat;
|
8
|
|
import java.text.DateFormatSymbols;
|
9
|
|
import java.text.FieldPosition;
|
10
|
|
import java.text.ParsePosition;
|
11
|
|
import java.text.SimpleDateFormat;
|
12
|
|
import java.util.Calendar;
|
13
|
|
import java.util.Date;
|
14
|
|
import java.util.Iterator;
|
15
|
|
import java.util.Locale;
|
16
|
|
|
17
|
|
import org.asyrinx.brownie.core.lang.StringUtils;
|
18
|
|
import org.asyrinx.brownie.core.util.Era;
|
19
|
|
import org.asyrinx.brownie.core.util.SimpleDate;
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
public class JpDateFormat extends DateFormat {
|
25
|
|
|
26
|
|
public static final String FORMAT_DATE_TIME_1 = "jEEjyy年MM月dd日 hh時mm分";
|
27
|
|
|
28
|
|
public static final String FORMAT_DATE_TIME_2 = "jEjyy年MM月dd日 hh時mm分";
|
29
|
|
|
30
|
|
public static final String FORMAT_DATE_TIME_3 = "jejyy/MM/dd hh:mm";
|
31
|
|
|
32
|
|
public static final String FORMAT_DATE_TIME_4 = "jEjyy/MM/dd hh:mm";
|
33
|
|
|
34
|
|
public static final String FORMAT_DATE_1 = "jEEjyy年MM月dd日";
|
35
|
|
|
36
|
|
public static final String FORMAT_DATE_2 = "jEjyy年MM月dd日";
|
37
|
|
|
38
|
|
public static final String FORMAT_DATE_3 = "jejyy/MM/dd";
|
39
|
|
|
40
|
|
public static final String FORMAT_DATE_4 = "jEjyy/MM/dd";
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
4
|
public JpDateFormat(String pattern) {
|
48
|
4
|
this(pattern, Locale.getDefault());
|
49
|
|
}
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
4
|
public JpDateFormat(String pattern, Locale locale) {
|
58
|
4
|
this(pattern, new DateFormatSymbols(locale));
|
59
|
|
}
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
4
|
public JpDateFormat(String pattern, DateFormatSymbols dateFormatSymbols) {
|
68
|
4
|
super();
|
69
|
4
|
this.pattern = pattern;
|
70
|
4
|
this.dateFormatSymbols = dateFormatSymbols;
|
71
|
|
}
|
72
|
|
|
73
|
|
private String pattern;
|
74
|
|
|
75
|
|
private DateFormatSymbols dateFormatSymbols;
|
76
|
|
|
77
|
12
|
public String toPattern() {
|
78
|
12
|
return this.pattern;
|
79
|
|
}
|
80
|
|
|
81
|
0
|
public void applyPattern(String p) {
|
82
|
0
|
this.pattern = p;
|
83
|
|
}
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
0
|
public DateFormatSymbols getDateFormatSymbols() {
|
89
|
0
|
return dateFormatSymbols;
|
90
|
|
}
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
0
|
public void setDateFormatSymbols(DateFormatSymbols symbols) {
|
96
|
0
|
dateFormatSymbols = symbols;
|
97
|
|
}
|
98
|
|
|
99
|
8
|
private DateFormat newDateFormat(String p) {
|
100
|
8
|
return new SimpleDateFormat(p, dateFormatSymbols);
|
101
|
|
}
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
|
|
111
|
4
|
public StringBuffer format(Date date, StringBuffer toAppendTo,
|
112
|
|
FieldPosition pos) {
|
113
|
4
|
if (date == null)
|
114
|
0
|
throw new NullPointerException("failed to format. date was null.");
|
115
|
4
|
final Era era = JpEra.ERA_GROUP.getEra(new SimpleDate(date));
|
116
|
4
|
final StringBuffer patternBuf = new StringBuffer(toPattern());
|
117
|
4
|
for (int i = 0; i < PATTERNS.length; i++) {
|
118
|
16
|
final JpPattern jpPattern = PATTERNS[i];
|
119
|
16
|
if (jpPattern.contained(patternBuf.toString()))
|
120
|
6
|
jpPattern.format(patternBuf, era, date);
|
121
|
|
}
|
122
|
4
|
final DateFormat innerFormat = newDateFormat(patternBuf.toString());
|
123
|
4
|
return innerFormat.format(date, toAppendTo, pos);
|
124
|
|
}
|
125
|
|
|
126
|
|
|
127
|
|
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
4
|
public Date parse(String text, ParsePosition pos) {
|
134
|
4
|
final StringBuffer patternBuf = new StringBuffer(toPattern());
|
135
|
4
|
final StringBuffer textBuf = new StringBuffer(text);
|
136
|
4
|
final JpEraPattern eraPattern = findEraPattern(toPattern(), pos
|
137
|
|
.getIndex());
|
138
|
4
|
final Era era = (eraPattern == null) ? null : eraPattern.findEra(
|
139
|
|
textBuf, patternBuf, pos.getIndex());
|
140
|
4
|
for (int i = 0; i < PATTERNS_YEAR.length; i++)
|
141
|
4
|
StringUtils.replace(patternBuf, PATTERNS_YEAR[i].pattern, "yyyy");
|
142
|
4
|
final DateFormat innerFormat = newDateFormat(patternBuf.toString());
|
143
|
4
|
final Calendar cal = Calendar.getInstance();
|
144
|
4
|
cal.set(Calendar.YEAR, 0);
|
145
|
4
|
final Date superResult = innerFormat.parse(textBuf.toString(), pos);
|
146
|
4
|
if (superResult != null)
|
147
|
4
|
cal.setTime(superResult);
|
148
|
4
|
if (era != null)
|
149
|
3
|
cal.set(Calendar.YEAR, era.toAnnoDomini(cal.get(Calendar.YEAR)));
|
150
|
4
|
return cal.getTime();
|
151
|
|
}
|
152
|
|
|
153
|
4
|
private JpEraPattern findEraPattern(String text, int position) {
|
154
|
4
|
for (int i = 0; i < PATTERNS_ERA.length; i++) {
|
155
|
9
|
final JpEraPattern eraPattern = PATTERNS_ERA[i];
|
156
|
9
|
int pos = text.indexOf(eraPattern.pattern, position);
|
157
|
9
|
if (pos > -1)
|
158
|
3
|
return PATTERNS_ERA[i];
|
159
|
|
}
|
160
|
1
|
return null;
|
161
|
|
}
|
162
|
|
|
163
|
|
static public final JpEraPattern ERA_ALPH_SHORT = new JpEraPattern("je") {
|
164
|
1
|
public boolean needSingleQuote() {
|
165
|
1
|
return true;
|
166
|
|
}
|
167
|
|
|
168
|
4
|
protected String toEraStr(Era era) {
|
169
|
4
|
return era.getFirstLetter();
|
170
|
|
}
|
171
|
|
};
|
172
|
|
|
173
|
|
static public final JpEraPattern ERA_KANJI_SHORT = new JpEraPattern("jE") {
|
174
|
1
|
public boolean needSingleQuote() {
|
175
|
1
|
return true;
|
176
|
|
}
|
177
|
|
|
178
|
4
|
protected String toEraStr(Era era) {
|
179
|
4
|
return era.getCaptionShort();
|
180
|
|
}
|
181
|
|
};
|
182
|
|
|
183
|
|
static public final JpEraPattern ERA_KANJI_LONG = new JpEraPattern("jEE") {
|
184
|
1
|
public boolean needSingleQuote() {
|
185
|
1
|
return true;
|
186
|
|
}
|
187
|
|
|
188
|
5
|
protected String toEraStr(Era era) {
|
189
|
5
|
return era.getCaption();
|
190
|
|
}
|
191
|
|
};
|
192
|
|
|
193
|
|
static public final JpPattern YEAR_NUMERIC = new JpPattern("jyy") {
|
194
|
3
|
public String newString(Era era, Date d) {
|
195
|
3
|
return String
|
196
|
|
.valueOf(JpEra.ERA_GROUP.getEraYear(new SimpleDate(d)));
|
197
|
|
}
|
198
|
|
|
199
|
3
|
public boolean needSingleQuote() {
|
200
|
3
|
return false;
|
201
|
|
}
|
202
|
|
};
|
203
|
|
|
204
|
|
static private JpPattern[] PATTERNS = new JpPattern[] { ERA_ALPH_SHORT,
|
205
|
|
ERA_KANJI_LONG, ERA_KANJI_SHORT, YEAR_NUMERIC, };
|
206
|
|
|
207
|
|
static private JpEraPattern[] PATTERNS_ERA = new JpEraPattern[] {
|
208
|
|
ERA_ALPH_SHORT, ERA_KANJI_LONG, ERA_KANJI_SHORT };
|
209
|
|
|
210
|
|
static private JpPattern[] PATTERNS_YEAR = new JpPattern[] { YEAR_NUMERIC };
|
211
|
|
|
212
|
|
}
|
213
|
|
|
214
|
|
abstract class JpPattern {
|
215
|
|
protected String pattern;
|
216
|
|
|
217
|
4
|
public JpPattern(String pattern) {
|
218
|
4
|
super();
|
219
|
4
|
this.pattern = pattern;
|
220
|
|
}
|
221
|
|
|
222
|
16
|
public boolean contained(String target) {
|
223
|
16
|
return (target.indexOf(pattern) > -1);
|
224
|
|
}
|
225
|
|
|
226
|
6
|
public void format(StringBuffer value, Era era, Date d) {
|
227
|
6
|
StringUtils.replace(value, this.pattern, newPattern(era, d));
|
228
|
|
}
|
229
|
|
|
230
|
6
|
private String newPattern(Era era, Date d) {
|
231
|
6
|
String result = newString(era, d);
|
232
|
6
|
if (needSingleQuote()) {
|
233
|
3
|
result = StringUtils.toQuoted(result, '\'');
|
234
|
|
}
|
235
|
6
|
return result;
|
236
|
|
}
|
237
|
|
|
238
|
|
abstract public boolean needSingleQuote();
|
239
|
|
|
240
|
|
abstract public String newString(Era era, Date d);
|
241
|
|
}
|
242
|
|
|
243
|
|
abstract class JpEraPattern extends JpPattern {
|
244
|
3
|
public JpEraPattern(String pattern) {
|
245
|
3
|
super(pattern);
|
246
|
|
}
|
247
|
|
|
248
|
3
|
public String newString(Era era, Date d) {
|
249
|
3
|
return toEraStr(era);
|
250
|
|
}
|
251
|
|
|
252
|
3
|
public Era findEra(StringBuffer textBuf, StringBuffer patternBuf,
|
253
|
|
int textIndex) {
|
254
|
3
|
if (textBuf.length() < 1)
|
255
|
0
|
return null;
|
256
|
3
|
int eraIndex = patternBuf.toString().indexOf(pattern) + textIndex;
|
257
|
3
|
final Iterator iterator = JpEra.ERA_GROUP.getEras(Locale.JAPAN)
|
258
|
|
.iterator();
|
259
|
7
|
while (iterator.hasNext()) {
|
260
|
7
|
final Era result = (Era) iterator.next();
|
261
|
7
|
final String val = toEraStr(result);
|
262
|
7
|
final String textSub = textBuf.toString().substring(textIndex,
|
263
|
|
textIndex + val.length());
|
264
|
7
|
if (val.equals(textSub)) {
|
265
|
3
|
patternBuf.delete(eraIndex, eraIndex + pattern.length());
|
266
|
3
|
textBuf.delete(eraIndex, eraIndex + toEraStr(result).length());
|
267
|
3
|
return result;
|
268
|
|
}
|
269
|
|
}
|
270
|
0
|
return null;
|
271
|
|
}
|
272
|
|
|
273
|
|
abstract protected String toEraStr(Era era);
|
274
|
|
}
|