author | redestad |
Thu, 21 Apr 2016 13:39:53 +0200 | |
changeset 37593 | 824750ada3d6 |
parent 25991 | e48157b42439 |
child 37781 | 71ed5645f17c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.util.calendar; |
|
27 |
||
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
28 |
import java.security.AccessController; |
2 | 29 |
import java.util.TimeZone; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
25991
diff
changeset
|
30 |
import sun.security.action.GetPropertyAction; |
2 | 31 |
|
32 |
/** |
|
33 |
* |
|
34 |
* @author Masayoshi Okutsu |
|
35 |
* @since 1.6 |
|
36 |
*/ |
|
37 |
||
38 |
public class LocalGregorianCalendar extends BaseCalendar { |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
39 |
private static final Era[] JAPANESE_ERAS = { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
40 |
new Era("Meiji", "M", -3218832000000L, true), |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
41 |
new Era("Taisho", "T", -1812153600000L, true), |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
42 |
new Era("Showa", "S", -1357603200000L, true), |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
43 |
new Era("Heisei", "H", 600220800000L, true), |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
44 |
}; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
45 |
|
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
46 |
private static boolean isValidEra(Era newEra, Era[] eras) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
47 |
Era last = eras[eras.length - 1]; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
48 |
if (last.getSinceDate().getYear() >= newEra.getSinceDate().getYear()) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
49 |
return false; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
50 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
51 |
// The new era name should be unique. Its abbr may not. |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
52 |
String newName = newEra.getName(); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
53 |
for (Era era : eras) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
54 |
if (era.getName().equals(newName)) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
55 |
return false; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
56 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
57 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
58 |
return true; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
59 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
60 |
|
2 | 61 |
private String name; |
62 |
private Era[] eras; |
|
63 |
||
64 |
public static class Date extends BaseCalendar.Date { |
|
65 |
||
66 |
protected Date() { |
|
67 |
super(); |
|
68 |
} |
|
69 |
||
70 |
protected Date(TimeZone zone) { |
|
71 |
super(zone); |
|
72 |
} |
|
73 |
||
74 |
private int gregorianYear = FIELD_UNDEFINED; |
|
75 |
||
15658 | 76 |
@Override |
2 | 77 |
public Date setEra(Era era) { |
78 |
if (getEra() != era) { |
|
79 |
super.setEra(era); |
|
80 |
gregorianYear = FIELD_UNDEFINED; |
|
81 |
} |
|
82 |
return this; |
|
83 |
} |
|
84 |
||
15658 | 85 |
@Override |
2 | 86 |
public Date addYear(int localYear) { |
87 |
super.addYear(localYear); |
|
88 |
gregorianYear += localYear; |
|
89 |
return this; |
|
90 |
} |
|
91 |
||
15658 | 92 |
@Override |
2 | 93 |
public Date setYear(int localYear) { |
94 |
if (getYear() != localYear) { |
|
95 |
super.setYear(localYear); |
|
96 |
gregorianYear = FIELD_UNDEFINED; |
|
97 |
} |
|
98 |
return this; |
|
99 |
} |
|
100 |
||
15658 | 101 |
@Override |
2 | 102 |
public int getNormalizedYear() { |
103 |
return gregorianYear; |
|
104 |
} |
|
105 |
||
15658 | 106 |
@Override |
2 | 107 |
public void setNormalizedYear(int normalizedYear) { |
108 |
this.gregorianYear = normalizedYear; |
|
109 |
} |
|
110 |
||
111 |
void setLocalEra(Era era) { |
|
112 |
super.setEra(era); |
|
113 |
} |
|
114 |
||
115 |
void setLocalYear(int year) { |
|
116 |
super.setYear(year); |
|
117 |
} |
|
118 |
||
15658 | 119 |
@Override |
2 | 120 |
public String toString() { |
121 |
String time = super.toString(); |
|
122 |
time = time.substring(time.indexOf('T')); |
|
123 |
StringBuffer sb = new StringBuffer(); |
|
124 |
Era era = getEra(); |
|
125 |
if (era != null) { |
|
126 |
String abbr = era.getAbbreviation(); |
|
127 |
if (abbr != null) { |
|
128 |
sb.append(abbr); |
|
129 |
} |
|
130 |
} |
|
131 |
sb.append(getYear()).append('.'); |
|
132 |
CalendarUtils.sprintf0d(sb, getMonth(), 2).append('.'); |
|
133 |
CalendarUtils.sprintf0d(sb, getDayOfMonth(), 2); |
|
134 |
sb.append(time); |
|
135 |
return sb.toString(); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
static LocalGregorianCalendar getLocalGregorianCalendar(String name) { |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
140 |
// Only the Japanese calendar is supported. |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
141 |
if (!"japanese".equals(name)) { |
2 | 142 |
return null; |
143 |
} |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
144 |
|
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
145 |
// Append an era to the predefined eras if it's given by the property. |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
25991
diff
changeset
|
146 |
String prop = GetPropertyAction |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
25991
diff
changeset
|
147 |
.getProperty("jdk.calendar.japanese.supplemental.era"); |
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
148 |
if (prop != null) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
149 |
Era era = parseEraEntry(prop); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
150 |
if (era != null) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
151 |
if (isValidEra(era, JAPANESE_ERAS)) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
152 |
int length = JAPANESE_ERAS.length; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
153 |
Era[] eras = new Era[length + 1]; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
154 |
System.arraycopy(JAPANESE_ERAS, 0, eras, 0, length); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
155 |
eras[length] = era; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
156 |
return new LocalGregorianCalendar(name, eras); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
157 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
158 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
159 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
160 |
return new LocalGregorianCalendar(name, JAPANESE_ERAS); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
161 |
} |
2 | 162 |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
163 |
private static Era parseEraEntry(String entry) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
164 |
String[] keyValuePairs = entry.split(","); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
165 |
String eraName = null; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
166 |
boolean localTime = true; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
167 |
long since = 0; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
168 |
String abbr = null; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
169 |
|
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
170 |
for (String item : keyValuePairs) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
171 |
String[] keyvalue = item.split("="); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
172 |
if (keyvalue.length != 2) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
173 |
return null; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
174 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
175 |
String key = keyvalue[0].trim(); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
176 |
String value = keyvalue[1].trim(); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
177 |
switch (key) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
178 |
case "name": |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
179 |
eraName = value; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
180 |
break; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
181 |
case "since": |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
182 |
if (value.endsWith("u")) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
183 |
localTime = false; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
184 |
value = value.substring(0, value.length() - 1); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
185 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
186 |
try { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
187 |
since = Long.parseLong(value); |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
188 |
} catch (NumberFormatException e) { |
2 | 189 |
return null; |
190 |
} |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
191 |
break; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
192 |
case "abbr": |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
193 |
abbr = value; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
194 |
break; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
195 |
default: |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
196 |
return null; |
2 | 197 |
} |
198 |
} |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
199 |
if (eraName == null || eraName.isEmpty() |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
200 |
|| abbr == null || abbr.isEmpty()) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
201 |
return null; |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
202 |
} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
203 |
return new Era(eraName, abbr, since, localTime); |
2 | 204 |
} |
205 |
||
206 |
private LocalGregorianCalendar(String name, Era[] eras) { |
|
207 |
this.name = name; |
|
208 |
this.eras = eras; |
|
209 |
setEras(eras); |
|
210 |
} |
|
211 |
||
15658 | 212 |
@Override |
2 | 213 |
public String getName() { |
214 |
return name; |
|
215 |
} |
|
216 |
||
15658 | 217 |
@Override |
2 | 218 |
public Date getCalendarDate() { |
219 |
return getCalendarDate(System.currentTimeMillis(), newCalendarDate()); |
|
220 |
} |
|
221 |
||
15658 | 222 |
@Override |
2 | 223 |
public Date getCalendarDate(long millis) { |
224 |
return getCalendarDate(millis, newCalendarDate()); |
|
225 |
} |
|
226 |
||
15658 | 227 |
@Override |
2 | 228 |
public Date getCalendarDate(long millis, TimeZone zone) { |
229 |
return getCalendarDate(millis, newCalendarDate(zone)); |
|
230 |
} |
|
231 |
||
15658 | 232 |
@Override |
2 | 233 |
public Date getCalendarDate(long millis, CalendarDate date) { |
234 |
Date ldate = (Date) super.getCalendarDate(millis, date); |
|
235 |
return adjustYear(ldate, millis, ldate.getZoneOffset()); |
|
236 |
} |
|
237 |
||
238 |
private Date adjustYear(Date ldate, long millis, int zoneOffset) { |
|
239 |
int i; |
|
240 |
for (i = eras.length - 1; i >= 0; --i) { |
|
241 |
Era era = eras[i]; |
|
242 |
long since = era.getSince(null); |
|
243 |
if (era.isLocalTime()) { |
|
244 |
since -= zoneOffset; |
|
245 |
} |
|
246 |
if (millis >= since) { |
|
247 |
ldate.setLocalEra(era); |
|
248 |
int y = ldate.getNormalizedYear() - era.getSinceDate().getYear() + 1; |
|
249 |
ldate.setLocalYear(y); |
|
250 |
break; |
|
251 |
} |
|
252 |
} |
|
253 |
if (i < 0) { |
|
254 |
ldate.setLocalEra(null); |
|
255 |
ldate.setLocalYear(ldate.getNormalizedYear()); |
|
256 |
} |
|
257 |
ldate.setNormalized(true); |
|
258 |
return ldate; |
|
259 |
} |
|
260 |
||
15658 | 261 |
@Override |
2 | 262 |
public Date newCalendarDate() { |
263 |
return new Date(); |
|
264 |
} |
|
265 |
||
15658 | 266 |
@Override |
2 | 267 |
public Date newCalendarDate(TimeZone zone) { |
268 |
return new Date(zone); |
|
269 |
} |
|
270 |
||
15658 | 271 |
@Override |
2 | 272 |
public boolean validate(CalendarDate date) { |
273 |
Date ldate = (Date) date; |
|
274 |
Era era = ldate.getEra(); |
|
275 |
if (era != null) { |
|
276 |
if (!validateEra(era)) { |
|
277 |
return false; |
|
278 |
} |
|
15658 | 279 |
ldate.setNormalizedYear(era.getSinceDate().getYear() + ldate.getYear() - 1); |
17474 | 280 |
Date tmp = newCalendarDate(date.getZone()); |
281 |
tmp.setEra(era).setDate(date.getYear(), date.getMonth(), date.getDayOfMonth()); |
|
282 |
normalize(tmp); |
|
283 |
if (tmp.getEra() != era) { |
|
284 |
return false; |
|
15658 | 285 |
} |
2 | 286 |
} else { |
17474 | 287 |
if (date.getYear() >= eras[0].getSinceDate().getYear()) { |
288 |
return false; |
|
289 |
} |
|
2 | 290 |
ldate.setNormalizedYear(ldate.getYear()); |
291 |
} |
|
292 |
return super.validate(ldate); |
|
293 |
} |
|
294 |
||
295 |
private boolean validateEra(Era era) { |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
296 |
for (Era era1 : eras) { |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
297 |
if (era == era1) { |
2 | 298 |
return true; |
299 |
} |
|
300 |
} |
|
301 |
return false; |
|
302 |
} |
|
303 |
||
15658 | 304 |
@Override |
2 | 305 |
public boolean normalize(CalendarDate date) { |
306 |
if (date.isNormalized()) { |
|
307 |
return true; |
|
308 |
} |
|
309 |
||
310 |
normalizeYear(date); |
|
311 |
Date ldate = (Date) date; |
|
312 |
||
313 |
// Normalize it as a Gregorian date and get its millisecond value |
|
314 |
super.normalize(ldate); |
|
315 |
||
316 |
boolean hasMillis = false; |
|
317 |
long millis = 0; |
|
318 |
int year = ldate.getNormalizedYear(); |
|
319 |
int i; |
|
320 |
Era era = null; |
|
321 |
for (i = eras.length - 1; i >= 0; --i) { |
|
322 |
era = eras[i]; |
|
323 |
if (era.isLocalTime()) { |
|
324 |
CalendarDate sinceDate = era.getSinceDate(); |
|
325 |
int sinceYear = sinceDate.getYear(); |
|
326 |
if (year > sinceYear) { |
|
327 |
break; |
|
328 |
} |
|
329 |
if (year == sinceYear) { |
|
330 |
int month = ldate.getMonth(); |
|
331 |
int sinceMonth = sinceDate.getMonth(); |
|
332 |
if (month > sinceMonth) { |
|
333 |
break; |
|
334 |
} |
|
335 |
if (month == sinceMonth) { |
|
336 |
int day = ldate.getDayOfMonth(); |
|
337 |
int sinceDay = sinceDate.getDayOfMonth(); |
|
338 |
if (day > sinceDay) { |
|
339 |
break; |
|
340 |
} |
|
341 |
if (day == sinceDay) { |
|
342 |
long timeOfDay = ldate.getTimeOfDay(); |
|
343 |
long sinceTimeOfDay = sinceDate.getTimeOfDay(); |
|
344 |
if (timeOfDay >= sinceTimeOfDay) { |
|
345 |
break; |
|
346 |
} |
|
347 |
--i; |
|
348 |
break; |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
} else { |
|
353 |
if (!hasMillis) { |
|
354 |
millis = super.getTime(date); |
|
355 |
hasMillis = true; |
|
356 |
} |
|
357 |
||
358 |
long since = era.getSince(date.getZone()); |
|
359 |
if (millis >= since) { |
|
360 |
break; |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
if (i >= 0) { |
|
365 |
ldate.setLocalEra(era); |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
366 |
@SuppressWarnings("null") |
2 | 367 |
int y = ldate.getNormalizedYear() - era.getSinceDate().getYear() + 1; |
368 |
ldate.setLocalYear(y); |
|
369 |
} else { |
|
370 |
// Set Gregorian year with no era |
|
371 |
ldate.setEra(null); |
|
372 |
ldate.setLocalYear(year); |
|
373 |
ldate.setNormalizedYear(year); |
|
374 |
} |
|
375 |
ldate.setNormalized(true); |
|
376 |
return true; |
|
377 |
} |
|
378 |
||
15658 | 379 |
@Override |
2 | 380 |
void normalizeMonth(CalendarDate date) { |
381 |
normalizeYear(date); |
|
382 |
super.normalizeMonth(date); |
|
383 |
} |
|
384 |
||
385 |
void normalizeYear(CalendarDate date) { |
|
386 |
Date ldate = (Date) date; |
|
387 |
// Set the supposed-to-be-correct Gregorian year first |
|
388 |
// e.g., Showa 90 becomes 2015 (1926 + 90 - 1). |
|
389 |
Era era = ldate.getEra(); |
|
390 |
if (era == null || !validateEra(era)) { |
|
391 |
ldate.setNormalizedYear(ldate.getYear()); |
|
392 |
} else { |
|
393 |
ldate.setNormalizedYear(era.getSinceDate().getYear() + ldate.getYear() - 1); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
/** |
|
398 |
* Returns whether the specified Gregorian year is a leap year. |
|
399 |
* @see #isLeapYear(Era, int) |
|
400 |
*/ |
|
15658 | 401 |
@Override |
2 | 402 |
public boolean isLeapYear(int gregorianYear) { |
403 |
return CalendarUtils.isGregorianLeapYear(gregorianYear); |
|
404 |
} |
|
405 |
||
406 |
public boolean isLeapYear(Era era, int year) { |
|
407 |
if (era == null) { |
|
408 |
return isLeapYear(year); |
|
409 |
} |
|
410 |
int gyear = era.getSinceDate().getYear() + year - 1; |
|
411 |
return isLeapYear(gyear); |
|
412 |
} |
|
413 |
||
15658 | 414 |
@Override |
2 | 415 |
public void getCalendarDateFromFixedDate(CalendarDate date, long fixedDate) { |
416 |
Date ldate = (Date) date; |
|
417 |
super.getCalendarDateFromFixedDate(ldate, fixedDate); |
|
418 |
adjustYear(ldate, (fixedDate - EPOCH_OFFSET) * DAY_IN_MILLIS, 0); |
|
419 |
} |
|
420 |
} |