author | okutsu |
Thu, 07 Aug 2014 13:04:26 +0900 | |
changeset 25966 | 71ee1f15845e |
parent 17474 | 8c100beabcc0 |
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 java.util; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.ObjectInputStream; |
|
13583 | 30 |
import sun.util.locale.provider.CalendarDataUtility; |
2 | 31 |
import sun.util.calendar.BaseCalendar; |
32 |
import sun.util.calendar.CalendarDate; |
|
33 |
import sun.util.calendar.CalendarSystem; |
|
34 |
import sun.util.calendar.CalendarUtils; |
|
35 |
import sun.util.calendar.Era; |
|
36 |
import sun.util.calendar.Gregorian; |
|
37 |
import sun.util.calendar.LocalGregorianCalendar; |
|
38 |
import sun.util.calendar.ZoneInfo; |
|
39 |
||
40 |
/** |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
41 |
* {@code JapaneseImperialCalendar} implements a Japanese |
2 | 42 |
* calendar system in which the imperial era-based year numbering is |
43 |
* supported from the Meiji era. The following are the eras supported |
|
44 |
* by this calendar system. |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
45 |
* <pre>{@code |
2 | 46 |
* ERA value Era name Since (in Gregorian) |
47 |
* ------------------------------------------------------ |
|
48 |
* 0 N/A N/A |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
49 |
* 1 Meiji 1868-01-01T00:00:00 local time |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
50 |
* 2 Taisho 1912-07-30T00:00:00 local time |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
51 |
* 3 Showa 1926-12-25T00:00:00 local time |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
52 |
* 4 Heisei 1989-01-08T00:00:00 local time |
2 | 53 |
* ------------------------------------------------------ |
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
54 |
* }</pre> |
2 | 55 |
* |
56 |
* <p><code>ERA</code> value 0 specifies the years before Meiji and |
|
57 |
* the Gregorian year values are used. Unlike {@link |
|
58 |
* GregorianCalendar}, the Julian to Gregorian transition is not |
|
59 |
* supported because it doesn't make any sense to the Japanese |
|
60 |
* calendar systems used before Meiji. To represent the years before |
|
61 |
* Gregorian year 1, 0 and negative values are used. The Japanese |
|
62 |
* Imperial rescripts and government decrees don't specify how to deal |
|
63 |
* with time differences for applying the era transitions. This |
|
64 |
* calendar implementation assumes local time for all transitions. |
|
65 |
* |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
66 |
* <p>A new era can be specified using property |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
67 |
* jdk.calendar.japanese.supplemental.era. The new era is added to the |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
68 |
* predefined eras. The syntax of the property is as follows. |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
69 |
* <p><pre> |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
70 |
* {@code name=<name>,abbr=<abbr>,since=<time['u']>} |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
71 |
* </pre> |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
72 |
* where |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
73 |
* <dl> |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
74 |
* <dt>{@code <name>:}<dd>the full name of the new era (non-ASCII characters allowed) |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
75 |
* <dt>{@code <abbr>:}<dd>the abbreviation of the new era (non-ASCII characters allowed) |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
76 |
* <dt>{@code <time['u']>:}<dd>the start time of the new era represented by |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
77 |
* milliseconds from 1970-01-01T00:00:00 local time or UTC if {@code 'u'} is |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
78 |
* appended to the milliseconds value. (ASCII digits only) |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
79 |
* </dl> |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
80 |
* |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
81 |
* <p>If the given era is invalid, such as the since value before the |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
82 |
* beginning of the last predefined era, the given era will be |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
83 |
* ignored. |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
84 |
* |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
85 |
* <p>The following is an example of the property usage. |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
86 |
* <p><pre> |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
87 |
* java -Djdk.calendar.japanese.supplemental.era="name=NewEra,abbr=N,since=253374307200000" |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
88 |
* </pre> |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
89 |
* The property specifies an era change to NewEra at 9999-02-11T00:00:00 local time. |
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
90 |
* |
2 | 91 |
* @author Masayoshi Okutsu |
92 |
* @since 1.6 |
|
93 |
*/ |
|
94 |
class JapaneseImperialCalendar extends Calendar { |
|
95 |
/* |
|
96 |
* Implementation Notes |
|
97 |
* |
|
98 |
* This implementation uses |
|
99 |
* sun.util.calendar.LocalGregorianCalendar to perform most of the |
|
100 |
* calendar calculations. LocalGregorianCalendar is configurable |
|
101 |
* and reads <JRE_HOME>/lib/calendars.properties at the start-up. |
|
102 |
*/ |
|
103 |
||
104 |
/** |
|
105 |
* The ERA constant designating the era before Meiji. |
|
106 |
*/ |
|
107 |
public static final int BEFORE_MEIJI = 0; |
|
108 |
||
109 |
/** |
|
110 |
* The ERA constant designating the Meiji era. |
|
111 |
*/ |
|
112 |
public static final int MEIJI = 1; |
|
113 |
||
114 |
/** |
|
115 |
* The ERA constant designating the Taisho era. |
|
116 |
*/ |
|
117 |
public static final int TAISHO = 2; |
|
118 |
||
119 |
/** |
|
120 |
* The ERA constant designating the Showa era. |
|
121 |
*/ |
|
122 |
public static final int SHOWA = 3; |
|
123 |
||
124 |
/** |
|
125 |
* The ERA constant designating the Heisei era. |
|
126 |
*/ |
|
127 |
public static final int HEISEI = 4; |
|
128 |
||
129 |
private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian) |
|
130 |
||
131 |
// Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit |
|
132 |
// into ints, they must be longs in order to prevent arithmetic overflow |
|
133 |
// when performing (bug 4173516). |
|
134 |
private static final int ONE_SECOND = 1000; |
|
135 |
private static final int ONE_MINUTE = 60*ONE_SECOND; |
|
136 |
private static final int ONE_HOUR = 60*ONE_MINUTE; |
|
137 |
private static final long ONE_DAY = 24*ONE_HOUR; |
|
138 |
||
139 |
// Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton). |
|
140 |
private static final LocalGregorianCalendar jcal |
|
141 |
= (LocalGregorianCalendar) CalendarSystem.forName("japanese"); |
|
142 |
||
143 |
// Gregorian calendar instance. This is required because era |
|
144 |
// transition dates are given in Gregorian dates. |
|
145 |
private static final Gregorian gcal = CalendarSystem.getGregorianCalendar(); |
|
146 |
||
147 |
// The Era instance representing "before Meiji". |
|
148 |
private static final Era BEFORE_MEIJI_ERA = new Era("BeforeMeiji", "BM", Long.MIN_VALUE, false); |
|
149 |
||
150 |
// Imperial eras. The sun.util.calendar.LocalGregorianCalendar |
|
151 |
// doesn't have an Era representing before Meiji, which is |
|
152 |
// inconvenient for a Calendar. So, era[0] is a reference to |
|
153 |
// BEFORE_MEIJI_ERA. |
|
154 |
private static final Era[] eras; |
|
155 |
||
156 |
// Fixed date of the first date of each era. |
|
157 |
private static final long[] sinceFixedDates; |
|
158 |
||
159 |
/* |
|
160 |
* <pre> |
|
161 |
* Greatest Least |
|
162 |
* Field name Minimum Minimum Maximum Maximum |
|
163 |
* ---------- ------- ------- ------- ------- |
|
164 |
* ERA 0 0 1 1 |
|
165 |
* YEAR -292275055 1 ? ? |
|
166 |
* MONTH 0 0 11 11 |
|
167 |
* WEEK_OF_YEAR 1 1 52* 53 |
|
168 |
* WEEK_OF_MONTH 0 0 4* 6 |
|
169 |
* DAY_OF_MONTH 1 1 28* 31 |
|
170 |
* DAY_OF_YEAR 1 1 365* 366 |
|
171 |
* DAY_OF_WEEK 1 1 7 7 |
|
172 |
* DAY_OF_WEEK_IN_MONTH -1 -1 4* 6 |
|
173 |
* AM_PM 0 0 1 1 |
|
174 |
* HOUR 0 0 11 11 |
|
175 |
* HOUR_OF_DAY 0 0 23 23 |
|
176 |
* MINUTE 0 0 59 59 |
|
177 |
* SECOND 0 0 59 59 |
|
178 |
* MILLISECOND 0 0 999 999 |
|
179 |
* ZONE_OFFSET -13:00 -13:00 14:00 14:00 |
|
180 |
* DST_OFFSET 0:00 0:00 0:20 2:00 |
|
181 |
* </pre> |
|
182 |
* *: depends on eras |
|
183 |
*/ |
|
184 |
static final int MIN_VALUES[] = { |
|
185 |
0, // ERA |
|
186 |
-292275055, // YEAR |
|
187 |
JANUARY, // MONTH |
|
188 |
1, // WEEK_OF_YEAR |
|
189 |
0, // WEEK_OF_MONTH |
|
190 |
1, // DAY_OF_MONTH |
|
191 |
1, // DAY_OF_YEAR |
|
192 |
SUNDAY, // DAY_OF_WEEK |
|
193 |
1, // DAY_OF_WEEK_IN_MONTH |
|
194 |
AM, // AM_PM |
|
195 |
0, // HOUR |
|
196 |
0, // HOUR_OF_DAY |
|
197 |
0, // MINUTE |
|
198 |
0, // SECOND |
|
199 |
0, // MILLISECOND |
|
200 |
-13*ONE_HOUR, // ZONE_OFFSET (UNIX compatibility) |
|
201 |
0 // DST_OFFSET |
|
202 |
}; |
|
203 |
static final int LEAST_MAX_VALUES[] = { |
|
204 |
0, // ERA (initialized later) |
|
205 |
0, // YEAR (initialized later) |
|
206 |
JANUARY, // MONTH (Showa 64 ended in January.) |
|
207 |
0, // WEEK_OF_YEAR (Showa 1 has only 6 days which could be 0 weeks.) |
|
208 |
4, // WEEK_OF_MONTH |
|
209 |
28, // DAY_OF_MONTH |
|
210 |
0, // DAY_OF_YEAR (initialized later) |
|
211 |
SATURDAY, // DAY_OF_WEEK |
|
212 |
4, // DAY_OF_WEEK_IN |
|
213 |
PM, // AM_PM |
|
214 |
11, // HOUR |
|
215 |
23, // HOUR_OF_DAY |
|
216 |
59, // MINUTE |
|
217 |
59, // SECOND |
|
218 |
999, // MILLISECOND |
|
219 |
14*ONE_HOUR, // ZONE_OFFSET |
|
220 |
20*ONE_MINUTE // DST_OFFSET (historical least maximum) |
|
221 |
}; |
|
222 |
static final int MAX_VALUES[] = { |
|
223 |
0, // ERA |
|
224 |
292278994, // YEAR |
|
225 |
DECEMBER, // MONTH |
|
226 |
53, // WEEK_OF_YEAR |
|
227 |
6, // WEEK_OF_MONTH |
|
228 |
31, // DAY_OF_MONTH |
|
229 |
366, // DAY_OF_YEAR |
|
230 |
SATURDAY, // DAY_OF_WEEK |
|
231 |
6, // DAY_OF_WEEK_IN |
|
232 |
PM, // AM_PM |
|
233 |
11, // HOUR |
|
234 |
23, // HOUR_OF_DAY |
|
235 |
59, // MINUTE |
|
236 |
59, // SECOND |
|
237 |
999, // MILLISECOND |
|
238 |
14*ONE_HOUR, // ZONE_OFFSET |
|
239 |
2*ONE_HOUR // DST_OFFSET (double summer time) |
|
240 |
}; |
|
241 |
||
242 |
// Proclaim serialization compatibility with JDK 1.6 |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
243 |
@SuppressWarnings("FieldNameHidesFieldInSuperclass") |
2 | 244 |
private static final long serialVersionUID = -3364572813905467929L; |
245 |
||
246 |
static { |
|
247 |
Era[] es = jcal.getEras(); |
|
248 |
int length = es.length + 1; |
|
249 |
eras = new Era[length]; |
|
250 |
sinceFixedDates = new long[length]; |
|
251 |
||
252 |
// eras[BEFORE_MEIJI] and sinceFixedDate[BEFORE_MEIJI] are the |
|
253 |
// same as Gregorian. |
|
254 |
int index = BEFORE_MEIJI; |
|
255 |
sinceFixedDates[index] = gcal.getFixedDate(BEFORE_MEIJI_ERA.getSinceDate()); |
|
256 |
eras[index++] = BEFORE_MEIJI_ERA; |
|
257 |
for (Era e : es) { |
|
258 |
CalendarDate d = e.getSinceDate(); |
|
259 |
sinceFixedDates[index] = gcal.getFixedDate(d); |
|
260 |
eras[index++] = e; |
|
261 |
} |
|
262 |
||
263 |
LEAST_MAX_VALUES[ERA] = MAX_VALUES[ERA] = eras.length - 1; |
|
264 |
||
265 |
// Calculate the least maximum year and least day of Year |
|
266 |
// values. The following code assumes that there's at most one |
|
267 |
// era transition in a Gregorian year. |
|
268 |
int year = Integer.MAX_VALUE; |
|
269 |
int dayOfYear = Integer.MAX_VALUE; |
|
270 |
CalendarDate date = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
|
271 |
for (int i = 1; i < eras.length; i++) { |
|
272 |
long fd = sinceFixedDates[i]; |
|
273 |
CalendarDate transitionDate = eras[i].getSinceDate(); |
|
274 |
date.setDate(transitionDate.getYear(), BaseCalendar.JANUARY, 1); |
|
275 |
long fdd = gcal.getFixedDate(date); |
|
17474 | 276 |
if (fd != fdd) { |
277 |
dayOfYear = Math.min((int)(fd - fdd) + 1, dayOfYear); |
|
278 |
} |
|
2 | 279 |
date.setDate(transitionDate.getYear(), BaseCalendar.DECEMBER, 31); |
17474 | 280 |
fdd = gcal.getFixedDate(date); |
281 |
if (fd != fdd) { |
|
282 |
dayOfYear = Math.min((int)(fdd - fd) + 1, dayOfYear); |
|
283 |
} |
|
2 | 284 |
LocalGregorianCalendar.Date lgd = getCalendarDate(fd - 1); |
285 |
int y = lgd.getYear(); |
|
286 |
// Unless the first year starts from January 1, the actual |
|
287 |
// max value could be one year short. For example, if it's |
|
288 |
// Showa 63 January 8, 63 is the actual max value since |
|
289 |
// Showa 64 January 8 doesn't exist. |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
290 |
if (!(lgd.getMonth() == BaseCalendar.JANUARY && lgd.getDayOfMonth() == 1)) { |
2 | 291 |
y--; |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
292 |
} |
2 | 293 |
year = Math.min(y, year); |
294 |
} |
|
295 |
LEAST_MAX_VALUES[YEAR] = year; // Max year could be smaller than this value. |
|
296 |
LEAST_MAX_VALUES[DAY_OF_YEAR] = dayOfYear; |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* jdate always has a sun.util.calendar.LocalGregorianCalendar.Date instance to |
|
301 |
* avoid overhead of creating it for each calculation. |
|
302 |
*/ |
|
303 |
private transient LocalGregorianCalendar.Date jdate; |
|
304 |
||
305 |
/** |
|
306 |
* Temporary int[2] to get time zone offsets. zoneOffsets[0] gets |
|
307 |
* the GMT offset value and zoneOffsets[1] gets the daylight saving |
|
308 |
* value. |
|
309 |
*/ |
|
310 |
private transient int[] zoneOffsets; |
|
311 |
||
312 |
/** |
|
313 |
* Temporary storage for saving original fields[] values in |
|
314 |
* non-lenient mode. |
|
315 |
*/ |
|
316 |
private transient int[] originalFields; |
|
317 |
||
318 |
/** |
|
319 |
* Constructs a <code>JapaneseImperialCalendar</code> based on the current time |
|
320 |
* in the given time zone with the given locale. |
|
321 |
* |
|
322 |
* @param zone the given time zone. |
|
323 |
* @param aLocale the given locale. |
|
324 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
325 |
JapaneseImperialCalendar(TimeZone zone, Locale aLocale) { |
2 | 326 |
super(zone, aLocale); |
327 |
jdate = jcal.newCalendarDate(zone); |
|
328 |
setTimeInMillis(System.currentTimeMillis()); |
|
329 |
} |
|
330 |
||
331 |
/** |
|
15280
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
332 |
* Constructs an "empty" {@code JapaneseImperialCalendar}. |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
333 |
* |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
334 |
* @param zone the given time zone |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
335 |
* @param aLocale the given locale |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
336 |
* @param flag the flag requesting an empty instance |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
337 |
*/ |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
338 |
JapaneseImperialCalendar(TimeZone zone, Locale aLocale, boolean flag) { |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
339 |
super(zone, aLocale); |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
340 |
jdate = jcal.newCalendarDate(zone); |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
341 |
} |
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
342 |
|
d9e473e85f3c
4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents:
14765
diff
changeset
|
343 |
/** |
13583 | 344 |
* Returns {@code "japanese"} as the calendar type of this {@code |
345 |
* JapaneseImperialCalendar}. |
|
346 |
* |
|
347 |
* @return {@code "japanese"} |
|
348 |
*/ |
|
349 |
@Override |
|
350 |
public String getCalendarType() { |
|
351 |
return "japanese"; |
|
352 |
} |
|
353 |
||
354 |
/** |
|
2 | 355 |
* Compares this <code>JapaneseImperialCalendar</code> to the specified |
356 |
* <code>Object</code>. The result is <code>true</code> if and |
|
357 |
* only if the argument is a <code>JapaneseImperialCalendar</code> object |
|
358 |
* that represents the same time value (millisecond offset from |
|
359 |
* the <a href="Calendar.html#Epoch">Epoch</a>) under the same |
|
360 |
* <code>Calendar</code> parameters. |
|
361 |
* |
|
362 |
* @param obj the object to compare with. |
|
363 |
* @return <code>true</code> if this object is equal to <code>obj</code>; |
|
364 |
* <code>false</code> otherwise. |
|
365 |
* @see Calendar#compareTo(Calendar) |
|
366 |
*/ |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
367 |
@Override |
2 | 368 |
public boolean equals(Object obj) { |
369 |
return obj instanceof JapaneseImperialCalendar && |
|
370 |
super.equals(obj); |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Generates the hash code for this |
|
375 |
* <code>JapaneseImperialCalendar</code> object. |
|
376 |
*/ |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
377 |
@Override |
2 | 378 |
public int hashCode() { |
379 |
return super.hashCode() ^ jdate.hashCode(); |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* Adds the specified (signed) amount of time to the given calendar field, |
|
384 |
* based on the calendar's rules. |
|
385 |
* |
|
386 |
* <p><em>Add rule 1</em>. The value of <code>field</code> |
|
387 |
* after the call minus the value of <code>field</code> before the |
|
388 |
* call is <code>amount</code>, modulo any overflow that has occurred in |
|
389 |
* <code>field</code>. Overflow occurs when a field value exceeds its |
|
390 |
* range and, as a result, the next larger field is incremented or |
|
391 |
* decremented and the field value is adjusted back into its range.</p> |
|
392 |
* |
|
393 |
* <p><em>Add rule 2</em>. If a smaller field is expected to be |
|
394 |
* invariant, but it is impossible for it to be equal to its |
|
395 |
* prior value because of changes in its minimum or maximum after |
|
396 |
* <code>field</code> is changed, then its value is adjusted to be as close |
|
397 |
* as possible to its expected value. A smaller field represents a |
|
398 |
* smaller unit of time. <code>HOUR</code> is a smaller field than |
|
399 |
* <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields |
|
400 |
* that are not expected to be invariant. The calendar system |
|
401 |
* determines what fields are expected to be invariant.</p> |
|
402 |
* |
|
403 |
* @param field the calendar field. |
|
404 |
* @param amount the amount of date or time to be added to the field. |
|
405 |
* @exception IllegalArgumentException if <code>field</code> is |
|
406 |
* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown, |
|
407 |
* or if any calendar fields have out-of-range values in |
|
408 |
* non-lenient mode. |
|
409 |
*/ |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
410 |
@Override |
2 | 411 |
public void add(int field, int amount) { |
412 |
// If amount == 0, do nothing even the given field is out of |
|
413 |
// range. This is tested by JCK. |
|
414 |
if (amount == 0) { |
|
415 |
return; // Do nothing! |
|
416 |
} |
|
417 |
||
418 |
if (field < 0 || field >= ZONE_OFFSET) { |
|
419 |
throw new IllegalArgumentException(); |
|
420 |
} |
|
421 |
||
422 |
// Sync the time and calendar fields. |
|
423 |
complete(); |
|
424 |
||
425 |
if (field == YEAR) { |
|
426 |
LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone(); |
|
427 |
d.addYear(amount); |
|
428 |
pinDayOfMonth(d); |
|
429 |
set(ERA, getEraIndex(d)); |
|
430 |
set(YEAR, d.getYear()); |
|
431 |
set(MONTH, d.getMonth() - 1); |
|
432 |
set(DAY_OF_MONTH, d.getDayOfMonth()); |
|
433 |
} else if (field == MONTH) { |
|
434 |
LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone(); |
|
435 |
d.addMonth(amount); |
|
436 |
pinDayOfMonth(d); |
|
437 |
set(ERA, getEraIndex(d)); |
|
438 |
set(YEAR, d.getYear()); |
|
439 |
set(MONTH, d.getMonth() - 1); |
|
440 |
set(DAY_OF_MONTH, d.getDayOfMonth()); |
|
441 |
} else if (field == ERA) { |
|
442 |
int era = internalGet(ERA) + amount; |
|
443 |
if (era < 0) { |
|
444 |
era = 0; |
|
445 |
} else if (era > eras.length - 1) { |
|
446 |
era = eras.length - 1; |
|
447 |
} |
|
448 |
set(ERA, era); |
|
449 |
} else { |
|
450 |
long delta = amount; |
|
451 |
long timeOfDay = 0; |
|
452 |
switch (field) { |
|
453 |
// Handle the time fields here. Convert the given |
|
454 |
// amount to milliseconds and call setTimeInMillis. |
|
455 |
case HOUR: |
|
456 |
case HOUR_OF_DAY: |
|
457 |
delta *= 60 * 60 * 1000; // hours to milliseconds |
|
458 |
break; |
|
459 |
||
460 |
case MINUTE: |
|
461 |
delta *= 60 * 1000; // minutes to milliseconds |
|
462 |
break; |
|
463 |
||
464 |
case SECOND: |
|
465 |
delta *= 1000; // seconds to milliseconds |
|
466 |
break; |
|
467 |
||
468 |
case MILLISECOND: |
|
469 |
break; |
|
470 |
||
471 |
// Handle week, day and AM_PM fields which involves |
|
472 |
// time zone offset change adjustment. Convert the |
|
473 |
// given amount to the number of days. |
|
474 |
case WEEK_OF_YEAR: |
|
475 |
case WEEK_OF_MONTH: |
|
476 |
case DAY_OF_WEEK_IN_MONTH: |
|
477 |
delta *= 7; |
|
478 |
break; |
|
479 |
||
480 |
case DAY_OF_MONTH: // synonym of DATE |
|
481 |
case DAY_OF_YEAR: |
|
482 |
case DAY_OF_WEEK: |
|
483 |
break; |
|
484 |
||
485 |
case AM_PM: |
|
486 |
// Convert the amount to the number of days (delta) |
|
487 |
// and +12 or -12 hours (timeOfDay). |
|
488 |
delta = amount / 2; |
|
489 |
timeOfDay = 12 * (amount % 2); |
|
490 |
break; |
|
491 |
} |
|
492 |
||
493 |
// The time fields don't require time zone offset change |
|
494 |
// adjustment. |
|
495 |
if (field >= HOUR) { |
|
496 |
setTimeInMillis(time + delta); |
|
497 |
return; |
|
498 |
} |
|
499 |
||
500 |
// The rest of the fields (week, day or AM_PM fields) |
|
501 |
// require time zone offset (both GMT and DST) change |
|
502 |
// adjustment. |
|
503 |
||
504 |
// Translate the current time to the fixed date and time |
|
505 |
// of the day. |
|
506 |
long fd = cachedFixedDate; |
|
507 |
timeOfDay += internalGet(HOUR_OF_DAY); |
|
508 |
timeOfDay *= 60; |
|
509 |
timeOfDay += internalGet(MINUTE); |
|
510 |
timeOfDay *= 60; |
|
511 |
timeOfDay += internalGet(SECOND); |
|
512 |
timeOfDay *= 1000; |
|
513 |
timeOfDay += internalGet(MILLISECOND); |
|
514 |
if (timeOfDay >= ONE_DAY) { |
|
515 |
fd++; |
|
516 |
timeOfDay -= ONE_DAY; |
|
517 |
} else if (timeOfDay < 0) { |
|
518 |
fd--; |
|
519 |
timeOfDay += ONE_DAY; |
|
520 |
} |
|
521 |
||
522 |
fd += delta; // fd is the expected fixed date after the calculation |
|
523 |
int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET); |
|
524 |
setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset); |
|
525 |
zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET); |
|
526 |
// If the time zone offset has changed, then adjust the difference. |
|
527 |
if (zoneOffset != 0) { |
|
528 |
setTimeInMillis(time + zoneOffset); |
|
529 |
long fd2 = cachedFixedDate; |
|
530 |
// If the adjustment has changed the date, then take |
|
531 |
// the previous one. |
|
532 |
if (fd2 != fd) { |
|
533 |
setTimeInMillis(time - zoneOffset); |
|
534 |
} |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
||
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
539 |
@Override |
2 | 540 |
public void roll(int field, boolean up) { |
541 |
roll(field, up ? +1 : -1); |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* Adds a signed amount to the specified calendar field without changing larger fields. |
|
546 |
* A negative roll amount means to subtract from field without changing |
|
547 |
* larger fields. If the specified amount is 0, this method performs nothing. |
|
548 |
* |
|
549 |
* <p>This method calls {@link #complete()} before adding the |
|
550 |
* amount so that all the calendar fields are normalized. If there |
|
551 |
* is any calendar field having an out-of-range value in non-lenient mode, then an |
|
552 |
* <code>IllegalArgumentException</code> is thrown. |
|
553 |
* |
|
554 |
* @param field the calendar field. |
|
555 |
* @param amount the signed amount to add to <code>field</code>. |
|
556 |
* @exception IllegalArgumentException if <code>field</code> is |
|
557 |
* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown, |
|
558 |
* or if any calendar fields have out-of-range values in |
|
559 |
* non-lenient mode. |
|
560 |
* @see #roll(int,boolean) |
|
561 |
* @see #add(int,int) |
|
562 |
* @see #set(int,int) |
|
563 |
*/ |
|
25966
71ee1f15845e
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents:
17474
diff
changeset
|
564 |
@Override |
2 | 565 |
public void roll(int field, int amount) { |
566 |
// If amount == 0, do nothing even the given field is out of |
|
567 |
// range. This is tested by JCK. |
|
568 |
if (amount == 0) { |
|
569 |
return; |
|
570 |
} |
|
571 |
||
572 |
if (field < 0 || field >= ZONE_OFFSET) { |
|
573 |
throw new IllegalArgumentException(); |
|
574 |
} |
|
575 |
||
576 |
// Sync the time and calendar fields. |
|
577 |
complete(); |
|
578 |
||
579 |
int min = getMinimum(field); |
|
580 |
int max = getMaximum(field); |
|
581 |
||
582 |
switch (field) { |
|
583 |
case ERA: |
|
584 |
case AM_PM: |
|
585 |
case MINUTE: |
|
586 |
case SECOND: |
|
587 |
case MILLISECOND: |
|
588 |
// These fields are handled simply, since they have fixed |
|
589 |
// minima and maxima. Other fields are complicated, since |
|
590 |
// the range within they must roll varies depending on the |
|
591 |
// date, a time zone and the era transitions. |
|
592 |
break; |
|
593 |
||
594 |
case HOUR: |
|
595 |
case HOUR_OF_DAY: |
|
596 |
{ |
|
597 |
int unit = max + 1; // 12 or 24 hours |
|
598 |
int h = internalGet(field); |
|
599 |
int nh = (h + amount) % unit; |
|
600 |
if (nh < 0) { |
|
601 |
nh += unit; |
|
602 |
} |
|
603 |
time += ONE_HOUR * (nh - h); |
|
604 |
||
605 |
// The day might have changed, which could happen if |
|
606 |
// the daylight saving time transition brings it to |
|
607 |
// the next day, although it's very unlikely. But we |
|
608 |
// have to make sure not to change the larger fields. |
|
609 |
CalendarDate d = jcal.getCalendarDate(time, getZone()); |
|
610 |
if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) { |
|
611 |
d.setEra(jdate.getEra()); |
|
612 |
d.setDate(internalGet(YEAR), |
|
613 |
internalGet(MONTH) + 1, |
|
614 |
internalGet(DAY_OF_MONTH)); |
|
615 |
if (field == HOUR) { |
|
616 |
assert (internalGet(AM_PM) == PM); |
|
617 |
d.addHours(+12); // restore PM |
|
618 |
} |
|
619 |
time = jcal.getTime(d); |
|
620 |
} |
|
621 |
int hourOfDay = d.getHours(); |
|
622 |
internalSet(field, hourOfDay % unit); |
|
623 |
if (field == HOUR) { |
|
624 |
internalSet(HOUR_OF_DAY, hourOfDay); |
|
625 |
} else { |
|
626 |
internalSet(AM_PM, hourOfDay / 12); |
|
627 |
internalSet(HOUR, hourOfDay % 12); |
|
628 |
} |
|
629 |
||
630 |
// Time zone offset and/or daylight saving might have changed. |
|
631 |
int zoneOffset = d.getZoneOffset(); |
|
632 |
int saving = d.getDaylightSaving(); |
|
633 |
internalSet(ZONE_OFFSET, zoneOffset - saving); |
|
634 |
internalSet(DST_OFFSET, saving); |
|
635 |
return; |
|
636 |
} |
|
637 |
||
638 |
case YEAR: |
|
639 |
min = getActualMinimum(field); |
|
640 |
max = getActualMaximum(field); |
|
641 |
break; |
|
642 |
||
643 |
case MONTH: |
|
644 |
// Rolling the month involves both pinning the final value to [0, 11] |
|
645 |
// and adjusting the DAY_OF_MONTH if necessary. We only adjust the |
|
646 |
// DAY_OF_MONTH if, after updating the MONTH field, it is illegal. |
|
647 |
// E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>. |
|
648 |
{ |
|
649 |
if (!isTransitionYear(jdate.getNormalizedYear())) { |
|
650 |
int year = jdate.getYear(); |
|
651 |
if (year == getMaximum(YEAR)) { |
|
652 |
CalendarDate jd = jcal.getCalendarDate(time, getZone()); |
|
653 |
CalendarDate d = jcal.getCalendarDate(Long.MAX_VALUE, getZone()); |
|
654 |
max = d.getMonth() - 1; |
|
655 |
int n = getRolledValue(internalGet(field), amount, min, max); |
|
656 |
if (n == max) { |
|
657 |
// To avoid overflow, use an equivalent year. |
|
658 |
jd.addYear(-400); |
|
659 |
jd.setMonth(n + 1); |
|
660 |
if (jd.getDayOfMonth() > d.getDayOfMonth()) { |
|
661 |
jd.setDayOfMonth(d.getDayOfMonth()); |
|
662 |
jcal.normalize(jd); |
|
663 |
} |
|
664 |
if (jd.getDayOfMonth() == d.getDayOfMonth() |
|
665 |
&& jd.getTimeOfDay() > d.getTimeOfDay()) { |
|
666 |
jd.setMonth(n + 1); |
|
667 |
jd.setDayOfMonth(d.getDayOfMonth() - 1); |
|
668 |
jcal.normalize(jd); |
|
669 |
// Month may have changed by the normalization. |
|
670 |
n = jd.getMonth() - 1; |
|
671 |
} |
|
672 |
set(DAY_OF_MONTH, jd.getDayOfMonth()); |
|
673 |
} |
|
674 |
set(MONTH, n); |
|
675 |
} else if (year == getMinimum(YEAR)) { |
|
676 |
CalendarDate jd = jcal.getCalendarDate(time, getZone()); |
|
677 |
CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
678 |
min = d.getMonth() - 1; |
|
679 |
int n = getRolledValue(internalGet(field), amount, min, max); |
|
680 |
if (n == min) { |
|
681 |
// To avoid underflow, use an equivalent year. |
|
682 |
jd.addYear(+400); |
|
683 |
jd.setMonth(n + 1); |
|
684 |
if (jd.getDayOfMonth() < d.getDayOfMonth()) { |
|
685 |
jd.setDayOfMonth(d.getDayOfMonth()); |
|
686 |
jcal.normalize(jd); |
|
687 |
} |
|
688 |
if (jd.getDayOfMonth() == d.getDayOfMonth() |
|
689 |
&& jd.getTimeOfDay() < d.getTimeOfDay()) { |
|
690 |
jd.setMonth(n + 1); |
|
691 |
jd.setDayOfMonth(d.getDayOfMonth() + 1); |
|
692 |
jcal.normalize(jd); |
|
693 |
// Month may have changed by the normalization. |
|
694 |
n = jd.getMonth() - 1; |
|
695 |
} |
|
696 |
set(DAY_OF_MONTH, jd.getDayOfMonth()); |
|
697 |
} |
|
698 |
set(MONTH, n); |
|
699 |
} else { |
|
700 |
int mon = (internalGet(MONTH) + amount) % 12; |
|
701 |
if (mon < 0) { |
|
702 |
mon += 12; |
|
703 |
} |
|
704 |
set(MONTH, mon); |
|
705 |
||
706 |
// Keep the day of month in the range. We |
|
707 |
// don't want to spill over into the next |
|
708 |
// month; e.g., we don't want jan31 + 1 mo -> |
|
709 |
// feb31 -> mar3. |
|
710 |
int monthLen = monthLength(mon); |
|
711 |
if (internalGet(DAY_OF_MONTH) > monthLen) { |
|
712 |
set(DAY_OF_MONTH, monthLen); |
|
713 |
} |
|
714 |
} |
|
715 |
} else { |
|
716 |
int eraIndex = getEraIndex(jdate); |
|
717 |
CalendarDate transition = null; |
|
718 |
if (jdate.getYear() == 1) { |
|
719 |
transition = eras[eraIndex].getSinceDate(); |
|
720 |
min = transition.getMonth() - 1; |
|
721 |
} else { |
|
722 |
if (eraIndex < eras.length - 1) { |
|
723 |
transition = eras[eraIndex + 1].getSinceDate(); |
|
724 |
if (transition.getYear() == jdate.getNormalizedYear()) { |
|
725 |
max = transition.getMonth() - 1; |
|
726 |
if (transition.getDayOfMonth() == 1) { |
|
727 |
max--; |
|
728 |
} |
|
729 |
} |
|
730 |
} |
|
731 |
} |
|
732 |
||
733 |
if (min == max) { |
|
734 |
// The year has only one month. No need to |
|
735 |
// process further. (Showa Gan-nen (year 1) |
|
736 |
// and the last year have only one month.) |
|
737 |
return; |
|
738 |
} |
|
739 |
int n = getRolledValue(internalGet(field), amount, min, max); |
|
740 |
set(MONTH, n); |
|
741 |
if (n == min) { |
|
742 |
if (!(transition.getMonth() == BaseCalendar.JANUARY |
|
743 |
&& transition.getDayOfMonth() == 1)) { |
|
744 |
if (jdate.getDayOfMonth() < transition.getDayOfMonth()) { |
|
745 |
set(DAY_OF_MONTH, transition.getDayOfMonth()); |
|
746 |
} |
|
747 |
} |
|
748 |
} else if (n == max && (transition.getMonth() - 1 == n)) { |
|
749 |
int dom = transition.getDayOfMonth(); |
|
750 |
if (jdate.getDayOfMonth() >= dom) { |
|
751 |
set(DAY_OF_MONTH, dom - 1); |
|
752 |
} |
|
753 |
} |
|
754 |
} |
|
755 |
return; |
|
756 |
} |
|
757 |
||
758 |
case WEEK_OF_YEAR: |
|
759 |
{ |
|
760 |
int y = jdate.getNormalizedYear(); |
|
761 |
max = getActualMaximum(WEEK_OF_YEAR); |
|
762 |
set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK)); // update stamp[field] |
|
763 |
int woy = internalGet(WEEK_OF_YEAR); |
|
764 |
int value = woy + amount; |
|
765 |
if (!isTransitionYear(jdate.getNormalizedYear())) { |
|
766 |
int year = jdate.getYear(); |
|
767 |
if (year == getMaximum(YEAR)) { |
|
768 |
max = getActualMaximum(WEEK_OF_YEAR); |
|
769 |
} else if (year == getMinimum(YEAR)) { |
|
770 |
min = getActualMinimum(WEEK_OF_YEAR); |
|
771 |
max = getActualMaximum(WEEK_OF_YEAR); |
|
772 |
if (value > min && value < max) { |
|
773 |
set(WEEK_OF_YEAR, value); |
|
774 |
return; |
|
775 |
} |
|
776 |
||
777 |
} |
|
778 |
// If the new value is in between min and max |
|
779 |
// (exclusive), then we can use the value. |
|
780 |
if (value > min && value < max) { |
|
781 |
set(WEEK_OF_YEAR, value); |
|
782 |
return; |
|
783 |
} |
|
784 |
long fd = cachedFixedDate; |
|
785 |
// Make sure that the min week has the current DAY_OF_WEEK |
|
786 |
long day1 = fd - (7 * (woy - min)); |
|
787 |
if (year != getMinimum(YEAR)) { |
|
788 |
if (gcal.getYearFromFixedDate(day1) != y) { |
|
789 |
min++; |
|
790 |
} |
|
791 |
} else { |
|
792 |
CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
793 |
if (day1 < jcal.getFixedDate(d)) { |
|
794 |
min++; |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
// Make sure the same thing for the max week |
|
799 |
fd += 7 * (max - internalGet(WEEK_OF_YEAR)); |
|
800 |
if (gcal.getYearFromFixedDate(fd) != y) { |
|
801 |
max--; |
|
802 |
} |
|
803 |
break; |
|
804 |
} |
|
805 |
||
806 |
// Handle transition here. |
|
807 |
long fd = cachedFixedDate; |
|
808 |
long day1 = fd - (7 * (woy - min)); |
|
809 |
// Make sure that the min week has the current DAY_OF_WEEK |
|
810 |
LocalGregorianCalendar.Date d = getCalendarDate(day1); |
|
811 |
if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) { |
|
812 |
min++; |
|
813 |
} |
|
814 |
||
815 |
// Make sure the same thing for the max week |
|
816 |
fd += 7 * (max - woy); |
|
817 |
jcal.getCalendarDateFromFixedDate(d, fd); |
|
818 |
if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) { |
|
819 |
max--; |
|
820 |
} |
|
821 |
// value: the new WEEK_OF_YEAR which must be converted |
|
822 |
// to month and day of month. |
|
823 |
value = getRolledValue(woy, amount, min, max) - 1; |
|
824 |
d = getCalendarDate(day1 + value * 7); |
|
825 |
set(MONTH, d.getMonth() - 1); |
|
826 |
set(DAY_OF_MONTH, d.getDayOfMonth()); |
|
827 |
return; |
|
828 |
} |
|
829 |
||
830 |
case WEEK_OF_MONTH: |
|
831 |
{ |
|
832 |
boolean isTransitionYear = isTransitionYear(jdate.getNormalizedYear()); |
|
833 |
// dow: relative day of week from the first day of week |
|
834 |
int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek(); |
|
835 |
if (dow < 0) { |
|
836 |
dow += 7; |
|
837 |
} |
|
838 |
||
839 |
long fd = cachedFixedDate; |
|
840 |
long month1; // fixed date of the first day (usually 1) of the month |
|
841 |
int monthLength; // actual month length |
|
842 |
if (isTransitionYear) { |
|
843 |
month1 = getFixedDateMonth1(jdate, fd); |
|
844 |
monthLength = actualMonthLength(); |
|
845 |
} else { |
|
846 |
month1 = fd - internalGet(DAY_OF_MONTH) + 1; |
|
847 |
monthLength = jcal.getMonthLength(jdate); |
|
848 |
} |
|
849 |
||
850 |
// the first day of week of the month. |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
851 |
long monthDay1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(month1 + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
852 |
getFirstDayOfWeek()); |
2 | 853 |
// if the week has enough days to form a week, the |
854 |
// week starts from the previous month. |
|
855 |
if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) { |
|
856 |
monthDay1st -= 7; |
|
857 |
} |
|
858 |
max = getActualMaximum(field); |
|
859 |
||
860 |
// value: the new WEEK_OF_MONTH value |
|
861 |
int value = getRolledValue(internalGet(field), amount, 1, max) - 1; |
|
862 |
||
863 |
// nfd: fixed date of the rolled date |
|
864 |
long nfd = monthDay1st + value * 7 + dow; |
|
865 |
||
866 |
// Unlike WEEK_OF_YEAR, we need to change day of week if the |
|
867 |
// nfd is out of the month. |
|
868 |
if (nfd < month1) { |
|
869 |
nfd = month1; |
|
870 |
} else if (nfd >= (month1 + monthLength)) { |
|
871 |
nfd = month1 + monthLength - 1; |
|
872 |
} |
|
873 |
set(DAY_OF_MONTH, (int)(nfd - month1) + 1); |
|
874 |
return; |
|
875 |
} |
|
876 |
||
877 |
case DAY_OF_MONTH: |
|
878 |
{ |
|
879 |
if (!isTransitionYear(jdate.getNormalizedYear())) { |
|
880 |
max = jcal.getMonthLength(jdate); |
|
881 |
break; |
|
882 |
} |
|
883 |
||
884 |
// TODO: Need to change the spec to be usable DAY_OF_MONTH rolling... |
|
885 |
||
886 |
// Transition handling. We can't change year and era |
|
887 |
// values here due to the Calendar roll spec! |
|
888 |
long month1 = getFixedDateMonth1(jdate, cachedFixedDate); |
|
889 |
||
890 |
// It may not be a regular month. Convert the date and range to |
|
891 |
// the relative values, perform the roll, and |
|
892 |
// convert the result back to the rolled date. |
|
893 |
int value = getRolledValue((int)(cachedFixedDate - month1), amount, |
|
894 |
0, actualMonthLength() - 1); |
|
895 |
LocalGregorianCalendar.Date d = getCalendarDate(month1 + value); |
|
896 |
assert getEraIndex(d) == internalGetEra() |
|
897 |
&& d.getYear() == internalGet(YEAR) && d.getMonth()-1 == internalGet(MONTH); |
|
898 |
set(DAY_OF_MONTH, d.getDayOfMonth()); |
|
899 |
return; |
|
900 |
} |
|
901 |
||
902 |
case DAY_OF_YEAR: |
|
903 |
{ |
|
904 |
max = getActualMaximum(field); |
|
905 |
if (!isTransitionYear(jdate.getNormalizedYear())) { |
|
906 |
break; |
|
907 |
} |
|
908 |
||
909 |
// Handle transition. We can't change year and era values |
|
910 |
// here due to the Calendar roll spec. |
|
911 |
int value = getRolledValue(internalGet(DAY_OF_YEAR), amount, min, max); |
|
912 |
long jan0 = cachedFixedDate - internalGet(DAY_OF_YEAR); |
|
913 |
LocalGregorianCalendar.Date d = getCalendarDate(jan0 + value); |
|
914 |
assert getEraIndex(d) == internalGetEra() && d.getYear() == internalGet(YEAR); |
|
915 |
set(MONTH, d.getMonth() - 1); |
|
916 |
set(DAY_OF_MONTH, d.getDayOfMonth()); |
|
917 |
return; |
|
918 |
} |
|
919 |
||
920 |
case DAY_OF_WEEK: |
|
921 |
{ |
|
922 |
int normalizedYear = jdate.getNormalizedYear(); |
|
923 |
if (!isTransitionYear(normalizedYear) && !isTransitionYear(normalizedYear - 1)) { |
|
924 |
// If the week of year is in the same year, we can |
|
925 |
// just change DAY_OF_WEEK. |
|
926 |
int weekOfYear = internalGet(WEEK_OF_YEAR); |
|
927 |
if (weekOfYear > 1 && weekOfYear < 52) { |
|
928 |
set(WEEK_OF_YEAR, internalGet(WEEK_OF_YEAR)); |
|
929 |
max = SATURDAY; |
|
930 |
break; |
|
931 |
} |
|
932 |
} |
|
933 |
||
934 |
// We need to handle it in a different way around year |
|
935 |
// boundaries and in the transition year. Note that |
|
936 |
// changing era and year values violates the roll |
|
937 |
// rule: not changing larger calendar fields... |
|
938 |
amount %= 7; |
|
939 |
if (amount == 0) { |
|
940 |
return; |
|
941 |
} |
|
942 |
long fd = cachedFixedDate; |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
943 |
long dowFirst = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek()); |
2 | 944 |
fd += amount; |
945 |
if (fd < dowFirst) { |
|
946 |
fd += 7; |
|
947 |
} else if (fd >= dowFirst + 7) { |
|
948 |
fd -= 7; |
|
949 |
} |
|
950 |
LocalGregorianCalendar.Date d = getCalendarDate(fd); |
|
951 |
set(ERA, getEraIndex(d)); |
|
952 |
set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth()); |
|
953 |
return; |
|
954 |
} |
|
955 |
||
956 |
case DAY_OF_WEEK_IN_MONTH: |
|
957 |
{ |
|
958 |
min = 1; // after having normalized, min should be 1. |
|
959 |
if (!isTransitionYear(jdate.getNormalizedYear())) { |
|
960 |
int dom = internalGet(DAY_OF_MONTH); |
|
961 |
int monthLength = jcal.getMonthLength(jdate); |
|
962 |
int lastDays = monthLength % 7; |
|
963 |
max = monthLength / 7; |
|
964 |
int x = (dom - 1) % 7; |
|
965 |
if (x < lastDays) { |
|
966 |
max++; |
|
967 |
} |
|
968 |
set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK)); |
|
969 |
break; |
|
970 |
} |
|
971 |
||
972 |
// Transition year handling. |
|
973 |
long fd = cachedFixedDate; |
|
974 |
long month1 = getFixedDateMonth1(jdate, fd); |
|
975 |
int monthLength = actualMonthLength(); |
|
976 |
int lastDays = monthLength % 7; |
|
977 |
max = monthLength / 7; |
|
978 |
int x = (int)(fd - month1) % 7; |
|
979 |
if (x < lastDays) { |
|
980 |
max++; |
|
981 |
} |
|
982 |
int value = getRolledValue(internalGet(field), amount, min, max) - 1; |
|
983 |
fd = month1 + value * 7 + x; |
|
984 |
LocalGregorianCalendar.Date d = getCalendarDate(fd); |
|
985 |
set(DAY_OF_MONTH, d.getDayOfMonth()); |
|
986 |
return; |
|
987 |
} |
|
988 |
} |
|
989 |
||
990 |
set(field, getRolledValue(internalGet(field), amount, min, max)); |
|
991 |
} |
|
992 |
||
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
993 |
@Override |
2 | 994 |
public String getDisplayName(int field, int style, Locale locale) { |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
995 |
if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale, |
2 | 996 |
ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
997 |
return null; |
|
998 |
} |
|
999 |
||
13583 | 1000 |
int fieldValue = get(field); |
1001 |
||
2 | 1002 |
// "GanNen" is supported only in the LONG style. |
1003 |
if (field == YEAR |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1004 |
&& (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) { |
2 | 1005 |
return null; |
1006 |
} |
|
1007 |
||
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1008 |
String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field, |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1009 |
fieldValue, style, locale); |
13583 | 1010 |
// If the ERA value is null, then |
1011 |
// try to get its name or abbreviation from the Era instance. |
|
1012 |
if (name == null && field == ERA && fieldValue < eras.length) { |
|
1013 |
Era era = eras[fieldValue]; |
|
1014 |
name = (style == SHORT) ? era.getAbbreviation() : era.getName(); |
|
2 | 1015 |
} |
1016 |
return name; |
|
1017 |
} |
|
1018 |
||
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1019 |
@Override |
2 | 1020 |
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) { |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1021 |
if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale, |
2 | 1022 |
ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
1023 |
return null; |
|
1024 |
} |
|
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1025 |
Map<String, Integer> names; |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1026 |
names = CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale); |
13583 | 1027 |
// If strings[] has fewer than eras[], get more names from eras[]. |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1028 |
if (names != null) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1029 |
if (field == ERA) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1030 |
int size = names.size(); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1031 |
if (style == ALL_STYLES) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1032 |
Set<Integer> values = new HashSet<>(); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1033 |
// count unique era values |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1034 |
for (String key : names.keySet()) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1035 |
values.add(names.get(key)); |
13583 | 1036 |
} |
14765
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1037 |
size = values.size(); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1038 |
} |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1039 |
if (size < eras.length) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1040 |
int baseStyle = getBaseStyle(style); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1041 |
for (int i = size; i < eras.length; i++) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1042 |
Era era = eras[i]; |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1043 |
if (baseStyle == ALL_STYLES || baseStyle == SHORT |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1044 |
|| baseStyle == NARROW_FORMAT) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1045 |
names.put(era.getAbbreviation(), i); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1046 |
} |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1047 |
if (baseStyle == ALL_STYLES || baseStyle == LONG) { |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1048 |
names.put(era.getName(), i); |
0987999ed367
8000983: Support narrow display names for calendar fields
okutsu
parents:
14014
diff
changeset
|
1049 |
} |
2 | 1050 |
} |
1051 |
} |
|
1052 |
} |
|
1053 |
} |
|
13583 | 1054 |
return names; |
2 | 1055 |
} |
1056 |
||
1057 |
/** |
|
1058 |
* Returns the minimum value for the given calendar field of this |
|
1059 |
* <code>Calendar</code> instance. The minimum value is |
|
1060 |
* defined as the smallest value returned by the {@link |
|
1061 |
* Calendar#get(int) get} method for any possible time value, |
|
1062 |
* taking into consideration the current values of the |
|
1063 |
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, |
|
1064 |
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, |
|
1065 |
* and {@link Calendar#getTimeZone() getTimeZone} methods. |
|
1066 |
* |
|
1067 |
* @param field the calendar field. |
|
1068 |
* @return the minimum value for the given calendar field. |
|
1069 |
* @see #getMaximum(int) |
|
1070 |
* @see #getGreatestMinimum(int) |
|
1071 |
* @see #getLeastMaximum(int) |
|
1072 |
* @see #getActualMinimum(int) |
|
1073 |
* @see #getActualMaximum(int) |
|
1074 |
*/ |
|
1075 |
public int getMinimum(int field) { |
|
1076 |
return MIN_VALUES[field]; |
|
1077 |
} |
|
1078 |
||
1079 |
/** |
|
1080 |
* Returns the maximum value for the given calendar field of this |
|
1081 |
* <code>GregorianCalendar</code> instance. The maximum value is |
|
1082 |
* defined as the largest value returned by the {@link |
|
1083 |
* Calendar#get(int) get} method for any possible time value, |
|
1084 |
* taking into consideration the current values of the |
|
1085 |
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, |
|
1086 |
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, |
|
1087 |
* and {@link Calendar#getTimeZone() getTimeZone} methods. |
|
1088 |
* |
|
1089 |
* @param field the calendar field. |
|
1090 |
* @return the maximum value for the given calendar field. |
|
1091 |
* @see #getMinimum(int) |
|
1092 |
* @see #getGreatestMinimum(int) |
|
1093 |
* @see #getLeastMaximum(int) |
|
1094 |
* @see #getActualMinimum(int) |
|
1095 |
* @see #getActualMaximum(int) |
|
1096 |
*/ |
|
1097 |
public int getMaximum(int field) { |
|
1098 |
switch (field) { |
|
1099 |
case YEAR: |
|
1100 |
{ |
|
1101 |
// The value should depend on the time zone of this calendar. |
|
1102 |
LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, |
|
1103 |
getZone()); |
|
1104 |
return Math.max(LEAST_MAX_VALUES[YEAR], d.getYear()); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
return MAX_VALUES[field]; |
|
1108 |
} |
|
1109 |
||
1110 |
/** |
|
1111 |
* Returns the highest minimum value for the given calendar field |
|
1112 |
* of this <code>GregorianCalendar</code> instance. The highest |
|
1113 |
* minimum value is defined as the largest value returned by |
|
1114 |
* {@link #getActualMinimum(int)} for any possible time value, |
|
1115 |
* taking into consideration the current values of the |
|
1116 |
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, |
|
1117 |
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, |
|
1118 |
* and {@link Calendar#getTimeZone() getTimeZone} methods. |
|
1119 |
* |
|
1120 |
* @param field the calendar field. |
|
1121 |
* @return the highest minimum value for the given calendar field. |
|
1122 |
* @see #getMinimum(int) |
|
1123 |
* @see #getMaximum(int) |
|
1124 |
* @see #getLeastMaximum(int) |
|
1125 |
* @see #getActualMinimum(int) |
|
1126 |
* @see #getActualMaximum(int) |
|
1127 |
*/ |
|
1128 |
public int getGreatestMinimum(int field) { |
|
1129 |
return field == YEAR ? 1 : MIN_VALUES[field]; |
|
1130 |
} |
|
1131 |
||
1132 |
/** |
|
1133 |
* Returns the lowest maximum value for the given calendar field |
|
1134 |
* of this <code>GregorianCalendar</code> instance. The lowest |
|
1135 |
* maximum value is defined as the smallest value returned by |
|
1136 |
* {@link #getActualMaximum(int)} for any possible time value, |
|
1137 |
* taking into consideration the current values of the |
|
1138 |
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, |
|
1139 |
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, |
|
1140 |
* and {@link Calendar#getTimeZone() getTimeZone} methods. |
|
1141 |
* |
|
1142 |
* @param field the calendar field |
|
1143 |
* @return the lowest maximum value for the given calendar field. |
|
1144 |
* @see #getMinimum(int) |
|
1145 |
* @see #getMaximum(int) |
|
1146 |
* @see #getGreatestMinimum(int) |
|
1147 |
* @see #getActualMinimum(int) |
|
1148 |
* @see #getActualMaximum(int) |
|
1149 |
*/ |
|
1150 |
public int getLeastMaximum(int field) { |
|
1151 |
switch (field) { |
|
1152 |
case YEAR: |
|
1153 |
{ |
|
1154 |
return Math.min(LEAST_MAX_VALUES[YEAR], getMaximum(YEAR)); |
|
1155 |
} |
|
1156 |
} |
|
1157 |
return LEAST_MAX_VALUES[field]; |
|
1158 |
} |
|
1159 |
||
1160 |
/** |
|
1161 |
* Returns the minimum value that this calendar field could have, |
|
1162 |
* taking into consideration the given time value and the current |
|
1163 |
* values of the |
|
1164 |
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, |
|
1165 |
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, |
|
1166 |
* and {@link Calendar#getTimeZone() getTimeZone} methods. |
|
1167 |
* |
|
1168 |
* @param field the calendar field |
|
1169 |
* @return the minimum of the given field for the time value of |
|
1170 |
* this <code>JapaneseImperialCalendar</code> |
|
1171 |
* @see #getMinimum(int) |
|
1172 |
* @see #getMaximum(int) |
|
1173 |
* @see #getGreatestMinimum(int) |
|
1174 |
* @see #getLeastMaximum(int) |
|
1175 |
* @see #getActualMaximum(int) |
|
1176 |
*/ |
|
1177 |
public int getActualMinimum(int field) { |
|
1178 |
if (!isFieldSet(YEAR_MASK|MONTH_MASK|WEEK_OF_YEAR_MASK, field)) { |
|
1179 |
return getMinimum(field); |
|
1180 |
} |
|
1181 |
||
1182 |
int value = 0; |
|
1183 |
JapaneseImperialCalendar jc = getNormalizedCalendar(); |
|
1184 |
// Get a local date which includes time of day and time zone, |
|
1185 |
// which are missing in jc.jdate. |
|
1186 |
LocalGregorianCalendar.Date jd = jcal.getCalendarDate(jc.getTimeInMillis(), |
|
1187 |
getZone()); |
|
1188 |
int eraIndex = getEraIndex(jd); |
|
1189 |
switch (field) { |
|
1190 |
case YEAR: |
|
1191 |
{ |
|
1192 |
if (eraIndex > BEFORE_MEIJI) { |
|
1193 |
value = 1; |
|
1194 |
long since = eras[eraIndex].getSince(getZone()); |
|
1195 |
CalendarDate d = jcal.getCalendarDate(since, getZone()); |
|
1196 |
// Use the same year in jd to take care of leap |
|
1197 |
// years. i.e., both jd and d must agree on leap |
|
1198 |
// or common years. |
|
1199 |
jd.setYear(d.getYear()); |
|
1200 |
jcal.normalize(jd); |
|
1201 |
assert jd.isLeapYear() == d.isLeapYear(); |
|
1202 |
if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) { |
|
1203 |
value++; |
|
1204 |
} |
|
1205 |
} else { |
|
1206 |
value = getMinimum(field); |
|
1207 |
CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
1208 |
// Use an equvalent year of d.getYear() if |
|
1209 |
// possible. Otherwise, ignore the leap year and |
|
1210 |
// common year difference. |
|
1211 |
int y = d.getYear(); |
|
1212 |
if (y > 400) { |
|
1213 |
y -= 400; |
|
1214 |
} |
|
1215 |
jd.setYear(y); |
|
1216 |
jcal.normalize(jd); |
|
1217 |
if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) { |
|
1218 |
value++; |
|
1219 |
} |
|
1220 |
} |
|
1221 |
} |
|
1222 |
break; |
|
1223 |
||
1224 |
case MONTH: |
|
1225 |
{ |
|
1226 |
// In Before Meiji and Meiji, January is the first month. |
|
1227 |
if (eraIndex > MEIJI && jd.getYear() == 1) { |
|
1228 |
long since = eras[eraIndex].getSince(getZone()); |
|
1229 |
CalendarDate d = jcal.getCalendarDate(since, getZone()); |
|
1230 |
value = d.getMonth() - 1; |
|
1231 |
if (jd.getDayOfMonth() < d.getDayOfMonth()) { |
|
1232 |
value++; |
|
1233 |
} |
|
1234 |
} |
|
1235 |
} |
|
1236 |
break; |
|
1237 |
||
1238 |
case WEEK_OF_YEAR: |
|
1239 |
{ |
|
1240 |
value = 1; |
|
1241 |
CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
1242 |
// shift 400 years to avoid underflow |
|
1243 |
d.addYear(+400); |
|
1244 |
jcal.normalize(d); |
|
1245 |
jd.setEra(d.getEra()); |
|
1246 |
jd.setYear(d.getYear()); |
|
1247 |
jcal.normalize(jd); |
|
1248 |
||
1249 |
long jan1 = jcal.getFixedDate(d); |
|
1250 |
long fd = jcal.getFixedDate(jd); |
|
1251 |
int woy = getWeekNumber(jan1, fd); |
|
1252 |
long day1 = fd - (7 * (woy - 1)); |
|
1253 |
if ((day1 < jan1) || |
|
1254 |
(day1 == jan1 && |
|
1255 |
jd.getTimeOfDay() < d.getTimeOfDay())) { |
|
1256 |
value++; |
|
1257 |
} |
|
1258 |
} |
|
1259 |
break; |
|
1260 |
} |
|
1261 |
return value; |
|
1262 |
} |
|
1263 |
||
1264 |
/** |
|
1265 |
* Returns the maximum value that this calendar field could have, |
|
1266 |
* taking into consideration the given time value and the current |
|
1267 |
* values of the |
|
1268 |
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, |
|
1269 |
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, |
|
1270 |
* and |
|
1271 |
* {@link Calendar#getTimeZone() getTimeZone} methods. |
|
1272 |
* For example, if the date of this instance is Heisei 16February 1, |
|
1273 |
* the actual maximum value of the <code>DAY_OF_MONTH</code> field |
|
1274 |
* is 29 because Heisei 16 is a leap year, and if the date of this |
|
1275 |
* instance is Heisei 17 February 1, it's 28. |
|
1276 |
* |
|
1277 |
* @param field the calendar field |
|
1278 |
* @return the maximum of the given field for the time value of |
|
1279 |
* this <code>JapaneseImperialCalendar</code> |
|
1280 |
* @see #getMinimum(int) |
|
1281 |
* @see #getMaximum(int) |
|
1282 |
* @see #getGreatestMinimum(int) |
|
1283 |
* @see #getLeastMaximum(int) |
|
1284 |
* @see #getActualMinimum(int) |
|
1285 |
*/ |
|
1286 |
public int getActualMaximum(int field) { |
|
1287 |
final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK| |
|
1288 |
HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK| |
|
1289 |
ZONE_OFFSET_MASK|DST_OFFSET_MASK; |
|
1290 |
if ((fieldsForFixedMax & (1<<field)) != 0) { |
|
1291 |
return getMaximum(field); |
|
1292 |
} |
|
1293 |
||
1294 |
JapaneseImperialCalendar jc = getNormalizedCalendar(); |
|
1295 |
LocalGregorianCalendar.Date date = jc.jdate; |
|
1296 |
int normalizedYear = date.getNormalizedYear(); |
|
1297 |
||
1298 |
int value = -1; |
|
1299 |
switch (field) { |
|
1300 |
case MONTH: |
|
1301 |
{ |
|
1302 |
value = DECEMBER; |
|
1303 |
if (isTransitionYear(date.getNormalizedYear())) { |
|
1304 |
// TODO: there may be multiple transitions in a year. |
|
1305 |
int eraIndex = getEraIndex(date); |
|
1306 |
if (date.getYear() != 1) { |
|
1307 |
eraIndex++; |
|
1308 |
assert eraIndex < eras.length; |
|
1309 |
} |
|
1310 |
long transition = sinceFixedDates[eraIndex]; |
|
1311 |
long fd = jc.cachedFixedDate; |
|
1312 |
if (fd < transition) { |
|
1313 |
LocalGregorianCalendar.Date ldate |
|
1314 |
= (LocalGregorianCalendar.Date) date.clone(); |
|
1315 |
jcal.getCalendarDateFromFixedDate(ldate, transition - 1); |
|
1316 |
value = ldate.getMonth() - 1; |
|
1317 |
} |
|
1318 |
} else { |
|
1319 |
LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, |
|
1320 |
getZone()); |
|
1321 |
if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) { |
|
1322 |
value = d.getMonth() - 1; |
|
1323 |
} |
|
1324 |
} |
|
1325 |
} |
|
1326 |
break; |
|
1327 |
||
1328 |
case DAY_OF_MONTH: |
|
1329 |
value = jcal.getMonthLength(date); |
|
1330 |
break; |
|
1331 |
||
1332 |
case DAY_OF_YEAR: |
|
1333 |
{ |
|
1334 |
if (isTransitionYear(date.getNormalizedYear())) { |
|
1335 |
// Handle transition year. |
|
1336 |
// TODO: there may be multiple transitions in a year. |
|
1337 |
int eraIndex = getEraIndex(date); |
|
1338 |
if (date.getYear() != 1) { |
|
1339 |
eraIndex++; |
|
1340 |
assert eraIndex < eras.length; |
|
1341 |
} |
|
1342 |
long transition = sinceFixedDates[eraIndex]; |
|
1343 |
long fd = jc.cachedFixedDate; |
|
1344 |
CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
|
1345 |
d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1); |
|
1346 |
if (fd < transition) { |
|
1347 |
value = (int)(transition - gcal.getFixedDate(d)); |
|
1348 |
} else { |
|
1349 |
d.addYear(+1); |
|
1350 |
value = (int)(gcal.getFixedDate(d) - transition); |
|
1351 |
} |
|
1352 |
} else { |
|
1353 |
LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, |
|
1354 |
getZone()); |
|
1355 |
if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) { |
|
1356 |
long fd = jcal.getFixedDate(d); |
|
1357 |
long jan1 = getFixedDateJan1(d, fd); |
|
1358 |
value = (int)(fd - jan1) + 1; |
|
1359 |
} else if (date.getYear() == getMinimum(YEAR)) { |
|
1360 |
CalendarDate d1 = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
1361 |
long fd1 = jcal.getFixedDate(d1); |
|
1362 |
d1.addYear(1); |
|
1363 |
d1.setMonth(BaseCalendar.JANUARY).setDayOfMonth(1); |
|
1364 |
jcal.normalize(d1); |
|
1365 |
long fd2 = jcal.getFixedDate(d1); |
|
1366 |
value = (int)(fd2 - fd1); |
|
1367 |
} else { |
|
1368 |
value = jcal.getYearLength(date); |
|
1369 |
} |
|
1370 |
} |
|
1371 |
} |
|
1372 |
break; |
|
1373 |
||
1374 |
case WEEK_OF_YEAR: |
|
1375 |
{ |
|
1376 |
if (!isTransitionYear(date.getNormalizedYear())) { |
|
1377 |
LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE, |
|
1378 |
getZone()); |
|
1379 |
if (date.getEra() == jd.getEra() && date.getYear() == jd.getYear()) { |
|
1380 |
long fd = jcal.getFixedDate(jd); |
|
1381 |
long jan1 = getFixedDateJan1(jd, fd); |
|
1382 |
value = getWeekNumber(jan1, fd); |
|
1383 |
} else if (date.getEra() == null && date.getYear() == getMinimum(YEAR)) { |
|
1384 |
CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
1385 |
// shift 400 years to avoid underflow |
|
1386 |
d.addYear(+400); |
|
1387 |
jcal.normalize(d); |
|
1388 |
jd.setEra(d.getEra()); |
|
1389 |
jd.setDate(d.getYear() + 1, BaseCalendar.JANUARY, 1); |
|
1390 |
jcal.normalize(jd); |
|
1391 |
long jan1 = jcal.getFixedDate(d); |
|
1392 |
long nextJan1 = jcal.getFixedDate(jd); |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1393 |
long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1394 |
getFirstDayOfWeek()); |
2 | 1395 |
int ndays = (int)(nextJan1st - nextJan1); |
1396 |
if (ndays >= getMinimalDaysInFirstWeek()) { |
|
1397 |
nextJan1st -= 7; |
|
1398 |
} |
|
1399 |
value = getWeekNumber(jan1, nextJan1st); |
|
1400 |
} else { |
|
1401 |
// Get the day of week of January 1 of the year |
|
1402 |
CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
|
1403 |
d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1); |
|
1404 |
int dayOfWeek = gcal.getDayOfWeek(d); |
|
1405 |
// Normalize the day of week with the firstDayOfWeek value |
|
1406 |
dayOfWeek -= getFirstDayOfWeek(); |
|
1407 |
if (dayOfWeek < 0) { |
|
1408 |
dayOfWeek += 7; |
|
1409 |
} |
|
1410 |
value = 52; |
|
1411 |
int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1; |
|
1412 |
if ((magic == 6) || |
|
1413 |
(date.isLeapYear() && (magic == 5 || magic == 12))) { |
|
1414 |
value++; |
|
1415 |
} |
|
1416 |
} |
|
1417 |
break; |
|
1418 |
} |
|
1419 |
||
1420 |
if (jc == this) { |
|
1421 |
jc = (JapaneseImperialCalendar) jc.clone(); |
|
1422 |
} |
|
1423 |
int max = getActualMaximum(DAY_OF_YEAR); |
|
1424 |
jc.set(DAY_OF_YEAR, max); |
|
1425 |
value = jc.get(WEEK_OF_YEAR); |
|
1426 |
if (value == 1 && max > 7) { |
|
1427 |
jc.add(WEEK_OF_YEAR, -1); |
|
1428 |
value = jc.get(WEEK_OF_YEAR); |
|
1429 |
} |
|
1430 |
} |
|
1431 |
break; |
|
1432 |
||
1433 |
case WEEK_OF_MONTH: |
|
1434 |
{ |
|
1435 |
LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE, |
|
1436 |
getZone()); |
|
1437 |
if (!(date.getEra() == jd.getEra() && date.getYear() == jd.getYear())) { |
|
1438 |
CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
|
1439 |
d.setDate(date.getNormalizedYear(), date.getMonth(), 1); |
|
1440 |
int dayOfWeek = gcal.getDayOfWeek(d); |
|
1441 |
int monthLength = gcal.getMonthLength(d); |
|
1442 |
dayOfWeek -= getFirstDayOfWeek(); |
|
1443 |
if (dayOfWeek < 0) { |
|
1444 |
dayOfWeek += 7; |
|
1445 |
} |
|
1446 |
int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week |
|
1447 |
value = 3; |
|
1448 |
if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) { |
|
1449 |
value++; |
|
1450 |
} |
|
1451 |
monthLength -= nDaysFirstWeek + 7 * 3; |
|
1452 |
if (monthLength > 0) { |
|
1453 |
value++; |
|
1454 |
if (monthLength > 7) { |
|
1455 |
value++; |
|
1456 |
} |
|
1457 |
} |
|
1458 |
} else { |
|
1459 |
long fd = jcal.getFixedDate(jd); |
|
1460 |
long month1 = fd - jd.getDayOfMonth() + 1; |
|
1461 |
value = getWeekNumber(month1, fd); |
|
1462 |
} |
|
1463 |
} |
|
1464 |
break; |
|
1465 |
||
1466 |
case DAY_OF_WEEK_IN_MONTH: |
|
1467 |
{ |
|
1468 |
int ndays, dow1; |
|
1469 |
int dow = date.getDayOfWeek(); |
|
1470 |
BaseCalendar.Date d = (BaseCalendar.Date) date.clone(); |
|
1471 |
ndays = jcal.getMonthLength(d); |
|
1472 |
d.setDayOfMonth(1); |
|
1473 |
jcal.normalize(d); |
|
1474 |
dow1 = d.getDayOfWeek(); |
|
1475 |
int x = dow - dow1; |
|
1476 |
if (x < 0) { |
|
1477 |
x += 7; |
|
1478 |
} |
|
1479 |
ndays -= x; |
|
1480 |
value = (ndays + 6) / 7; |
|
1481 |
} |
|
1482 |
break; |
|
1483 |
||
1484 |
case YEAR: |
|
1485 |
{ |
|
1486 |
CalendarDate jd = jcal.getCalendarDate(jc.getTimeInMillis(), getZone()); |
|
1487 |
CalendarDate d; |
|
1488 |
int eraIndex = getEraIndex(date); |
|
1489 |
if (eraIndex == eras.length - 1) { |
|
1490 |
d = jcal.getCalendarDate(Long.MAX_VALUE, getZone()); |
|
1491 |
value = d.getYear(); |
|
1492 |
// Use an equivalent year for the |
|
1493 |
// getYearOffsetInMillis call to avoid overflow. |
|
1494 |
if (value > 400) { |
|
1495 |
jd.setYear(value - 400); |
|
1496 |
} |
|
1497 |
} else { |
|
1498 |
d = jcal.getCalendarDate(eras[eraIndex + 1].getSince(getZone()) - 1, |
|
1499 |
getZone()); |
|
1500 |
value = d.getYear(); |
|
1501 |
// Use the same year as d.getYear() to be |
|
1502 |
// consistent with leap and common years. |
|
1503 |
jd.setYear(value); |
|
1504 |
} |
|
1505 |
jcal.normalize(jd); |
|
1506 |
if (getYearOffsetInMillis(jd) > getYearOffsetInMillis(d)) { |
|
1507 |
value--; |
|
1508 |
} |
|
1509 |
} |
|
1510 |
break; |
|
1511 |
||
1512 |
default: |
|
1513 |
throw new ArrayIndexOutOfBoundsException(field); |
|
1514 |
} |
|
1515 |
return value; |
|
1516 |
} |
|
1517 |
||
1518 |
/** |
|
1519 |
* Returns the millisecond offset from the beginning of the |
|
1520 |
* year. In the year for Long.MIN_VALUE, it's a pseudo value |
|
1521 |
* beyond the limit. The given CalendarDate object must have been |
|
1522 |
* normalized before calling this method. |
|
1523 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1524 |
private long getYearOffsetInMillis(CalendarDate date) { |
2 | 1525 |
long t = (jcal.getDayOfYear(date) - 1) * ONE_DAY; |
1526 |
return t + date.getTimeOfDay() - date.getZoneOffset(); |
|
1527 |
} |
|
1528 |
||
1529 |
public Object clone() { |
|
1530 |
JapaneseImperialCalendar other = (JapaneseImperialCalendar) super.clone(); |
|
1531 |
||
1532 |
other.jdate = (LocalGregorianCalendar.Date) jdate.clone(); |
|
1533 |
other.originalFields = null; |
|
1534 |
other.zoneOffsets = null; |
|
1535 |
return other; |
|
1536 |
} |
|
1537 |
||
1538 |
public TimeZone getTimeZone() { |
|
1539 |
TimeZone zone = super.getTimeZone(); |
|
1540 |
// To share the zone by the CalendarDate |
|
1541 |
jdate.setZone(zone); |
|
1542 |
return zone; |
|
1543 |
} |
|
1544 |
||
1545 |
public void setTimeZone(TimeZone zone) { |
|
1546 |
super.setTimeZone(zone); |
|
1547 |
// To share the zone by the CalendarDate |
|
1548 |
jdate.setZone(zone); |
|
1549 |
} |
|
1550 |
||
1551 |
/** |
|
1552 |
* The fixed date corresponding to jdate. If the value is |
|
1553 |
* Long.MIN_VALUE, the fixed date value is unknown. |
|
1554 |
*/ |
|
1555 |
transient private long cachedFixedDate = Long.MIN_VALUE; |
|
1556 |
||
1557 |
/** |
|
1558 |
* Converts the time value (millisecond offset from the <a |
|
1559 |
* href="Calendar.html#Epoch">Epoch</a>) to calendar field values. |
|
1560 |
* The time is <em>not</em> |
|
1561 |
* recomputed first; to recompute the time, then the fields, call the |
|
1562 |
* <code>complete</code> method. |
|
1563 |
* |
|
1564 |
* @see Calendar#complete |
|
1565 |
*/ |
|
1566 |
protected void computeFields() { |
|
1567 |
int mask = 0; |
|
1568 |
if (isPartiallyNormalized()) { |
|
1569 |
// Determine which calendar fields need to be computed. |
|
1570 |
mask = getSetStateFields(); |
|
1571 |
int fieldMask = ~mask & ALL_FIELDS; |
|
1572 |
if (fieldMask != 0 || cachedFixedDate == Long.MIN_VALUE) { |
|
1573 |
mask |= computeFields(fieldMask, |
|
1574 |
mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)); |
|
1575 |
assert mask == ALL_FIELDS; |
|
1576 |
} |
|
1577 |
} else { |
|
1578 |
// Specify all fields |
|
1579 |
mask = ALL_FIELDS; |
|
1580 |
computeFields(mask, 0); |
|
1581 |
} |
|
1582 |
// After computing all the fields, set the field state to `COMPUTED'. |
|
1583 |
setFieldsComputed(mask); |
|
1584 |
} |
|
1585 |
||
1586 |
/** |
|
1587 |
* This computeFields implements the conversion from UTC |
|
1588 |
* (millisecond offset from the Epoch) to calendar |
|
1589 |
* field values. fieldMask specifies which fields to change the |
|
1590 |
* setting state to COMPUTED, although all fields are set to |
|
1591 |
* the correct values. This is required to fix 4685354. |
|
1592 |
* |
|
1593 |
* @param fieldMask a bit mask to specify which fields to change |
|
1594 |
* the setting state. |
|
1595 |
* @param tzMask a bit mask to specify which time zone offset |
|
1596 |
* fields to be used for time calculations |
|
1597 |
* @return a new field mask that indicates what field values have |
|
1598 |
* actually been set. |
|
1599 |
*/ |
|
1600 |
private int computeFields(int fieldMask, int tzMask) { |
|
1601 |
int zoneOffset = 0; |
|
1602 |
TimeZone tz = getZone(); |
|
1603 |
if (zoneOffsets == null) { |
|
1604 |
zoneOffsets = new int[2]; |
|
1605 |
} |
|
1606 |
if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) { |
|
1607 |
if (tz instanceof ZoneInfo) { |
|
1608 |
zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets); |
|
1609 |
} else { |
|
1610 |
zoneOffset = tz.getOffset(time); |
|
1611 |
zoneOffsets[0] = tz.getRawOffset(); |
|
1612 |
zoneOffsets[1] = zoneOffset - zoneOffsets[0]; |
|
1613 |
} |
|
1614 |
} |
|
1615 |
if (tzMask != 0) { |
|
1616 |
if (isFieldSet(tzMask, ZONE_OFFSET)) { |
|
1617 |
zoneOffsets[0] = internalGet(ZONE_OFFSET); |
|
1618 |
} |
|
1619 |
if (isFieldSet(tzMask, DST_OFFSET)) { |
|
1620 |
zoneOffsets[1] = internalGet(DST_OFFSET); |
|
1621 |
} |
|
1622 |
zoneOffset = zoneOffsets[0] + zoneOffsets[1]; |
|
1623 |
} |
|
1624 |
||
1625 |
// By computing time and zoneOffset separately, we can take |
|
1626 |
// the wider range of time+zoneOffset than the previous |
|
1627 |
// implementation. |
|
1628 |
long fixedDate = zoneOffset / ONE_DAY; |
|
1629 |
int timeOfDay = zoneOffset % (int)ONE_DAY; |
|
1630 |
fixedDate += time / ONE_DAY; |
|
1631 |
timeOfDay += (int) (time % ONE_DAY); |
|
1632 |
if (timeOfDay >= ONE_DAY) { |
|
1633 |
timeOfDay -= ONE_DAY; |
|
1634 |
++fixedDate; |
|
1635 |
} else { |
|
1636 |
while (timeOfDay < 0) { |
|
1637 |
timeOfDay += ONE_DAY; |
|
1638 |
--fixedDate; |
|
1639 |
} |
|
1640 |
} |
|
1641 |
fixedDate += EPOCH_OFFSET; |
|
1642 |
||
1643 |
// See if we can use jdate to avoid date calculation. |
|
1644 |
if (fixedDate != cachedFixedDate || fixedDate < 0) { |
|
1645 |
jcal.getCalendarDateFromFixedDate(jdate, fixedDate); |
|
1646 |
cachedFixedDate = fixedDate; |
|
1647 |
} |
|
1648 |
int era = getEraIndex(jdate); |
|
1649 |
int year = jdate.getYear(); |
|
1650 |
||
1651 |
// Always set the ERA and YEAR values. |
|
1652 |
internalSet(ERA, era); |
|
1653 |
internalSet(YEAR, year); |
|
1654 |
int mask = fieldMask | (ERA_MASK|YEAR_MASK); |
|
1655 |
||
1656 |
int month = jdate.getMonth() - 1; // 0-based |
|
1657 |
int dayOfMonth = jdate.getDayOfMonth(); |
|
1658 |
||
1659 |
// Set the basic date fields. |
|
1660 |
if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK)) |
|
1661 |
!= 0) { |
|
1662 |
internalSet(MONTH, month); |
|
1663 |
internalSet(DAY_OF_MONTH, dayOfMonth); |
|
1664 |
internalSet(DAY_OF_WEEK, jdate.getDayOfWeek()); |
|
1665 |
mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK; |
|
1666 |
} |
|
1667 |
||
1668 |
if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK |
|
1669 |
|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) { |
|
1670 |
if (timeOfDay != 0) { |
|
1671 |
int hours = timeOfDay / ONE_HOUR; |
|
1672 |
internalSet(HOUR_OF_DAY, hours); |
|
1673 |
internalSet(AM_PM, hours / 12); // Assume AM == 0 |
|
1674 |
internalSet(HOUR, hours % 12); |
|
1675 |
int r = timeOfDay % ONE_HOUR; |
|
1676 |
internalSet(MINUTE, r / ONE_MINUTE); |
|
1677 |
r %= ONE_MINUTE; |
|
1678 |
internalSet(SECOND, r / ONE_SECOND); |
|
1679 |
internalSet(MILLISECOND, r % ONE_SECOND); |
|
1680 |
} else { |
|
1681 |
internalSet(HOUR_OF_DAY, 0); |
|
1682 |
internalSet(AM_PM, AM); |
|
1683 |
internalSet(HOUR, 0); |
|
1684 |
internalSet(MINUTE, 0); |
|
1685 |
internalSet(SECOND, 0); |
|
1686 |
internalSet(MILLISECOND, 0); |
|
1687 |
} |
|
1688 |
mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK |
|
1689 |
|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK); |
|
1690 |
} |
|
1691 |
||
1692 |
if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) { |
|
1693 |
internalSet(ZONE_OFFSET, zoneOffsets[0]); |
|
1694 |
internalSet(DST_OFFSET, zoneOffsets[1]); |
|
1695 |
mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK); |
|
1696 |
} |
|
1697 |
||
1698 |
if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK |
|
1699 |
|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) { |
|
1700 |
int normalizedYear = jdate.getNormalizedYear(); |
|
1701 |
// If it's a year of an era transition, we need to handle |
|
1702 |
// irregular year boundaries. |
|
1703 |
boolean transitionYear = isTransitionYear(jdate.getNormalizedYear()); |
|
1704 |
int dayOfYear; |
|
1705 |
long fixedDateJan1; |
|
1706 |
if (transitionYear) { |
|
1707 |
fixedDateJan1 = getFixedDateJan1(jdate, fixedDate); |
|
1708 |
dayOfYear = (int)(fixedDate - fixedDateJan1) + 1; |
|
1709 |
} else if (normalizedYear == MIN_VALUES[YEAR]) { |
|
1710 |
CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
1711 |
fixedDateJan1 = jcal.getFixedDate(dx); |
|
1712 |
dayOfYear = (int)(fixedDate - fixedDateJan1) + 1; |
|
1713 |
} else { |
|
1714 |
dayOfYear = (int) jcal.getDayOfYear(jdate); |
|
1715 |
fixedDateJan1 = fixedDate - dayOfYear + 1; |
|
1716 |
} |
|
1717 |
long fixedDateMonth1 = transitionYear ? |
|
1718 |
getFixedDateMonth1(jdate, fixedDate) : fixedDate - dayOfMonth + 1; |
|
1719 |
||
1720 |
internalSet(DAY_OF_YEAR, dayOfYear); |
|
1721 |
internalSet(DAY_OF_WEEK_IN_MONTH, (dayOfMonth - 1) / 7 + 1); |
|
1722 |
||
1723 |
int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate); |
|
1724 |
||
1725 |
// The spec is to calculate WEEK_OF_YEAR in the |
|
1726 |
// ISO8601-style. This creates problems, though. |
|
1727 |
if (weekOfYear == 0) { |
|
1728 |
// If the date belongs to the last week of the |
|
1729 |
// previous year, use the week number of "12/31" of |
|
1730 |
// the "previous" year. Again, if the previous year is |
|
1731 |
// a transition year, we need to take care of it. |
|
1732 |
// Usually the previous day of the first day of a year |
|
1733 |
// is December 31, which is not always true in the |
|
1734 |
// Japanese imperial calendar system. |
|
1735 |
long fixedDec31 = fixedDateJan1 - 1; |
|
1736 |
long prevJan1; |
|
1737 |
LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31); |
|
1738 |
if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) { |
|
1739 |
prevJan1 = fixedDateJan1 - 365; |
|
1740 |
if (d.isLeapYear()) { |
|
1741 |
--prevJan1; |
|
1742 |
} |
|
1743 |
} else if (transitionYear) { |
|
1744 |
if (jdate.getYear() == 1) { |
|
1745 |
// As of Heisei (since Meiji) there's no case |
|
1746 |
// that there are multiple transitions in a |
|
1747 |
// year. Historically there was such |
|
1748 |
// case. There might be such case again in the |
|
1749 |
// future. |
|
1750 |
if (era > HEISEI) { |
|
1751 |
CalendarDate pd = eras[era - 1].getSinceDate(); |
|
1752 |
if (normalizedYear == pd.getYear()) { |
|
1753 |
d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth()); |
|
1754 |
} |
|
1755 |
} else { |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1756 |
d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1); |
2 | 1757 |
} |
1758 |
jcal.normalize(d); |
|
1759 |
prevJan1 = jcal.getFixedDate(d); |
|
1760 |
} else { |
|
1761 |
prevJan1 = fixedDateJan1 - 365; |
|
1762 |
if (d.isLeapYear()) { |
|
1763 |
--prevJan1; |
|
1764 |
} |
|
1765 |
} |
|
1766 |
} else { |
|
1767 |
CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate(); |
|
1768 |
d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth()); |
|
1769 |
jcal.normalize(d); |
|
1770 |
prevJan1 = jcal.getFixedDate(d); |
|
1771 |
} |
|
1772 |
weekOfYear = getWeekNumber(prevJan1, fixedDec31); |
|
1773 |
} else { |
|
1774 |
if (!transitionYear) { |
|
1775 |
// Regular years |
|
1776 |
if (weekOfYear >= 52) { |
|
1777 |
long nextJan1 = fixedDateJan1 + 365; |
|
1778 |
if (jdate.isLeapYear()) { |
|
1779 |
nextJan1++; |
|
1780 |
} |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1781 |
long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1782 |
getFirstDayOfWeek()); |
2 | 1783 |
int ndays = (int)(nextJan1st - nextJan1); |
1784 |
if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) { |
|
1785 |
// The first days forms a week in which the date is included. |
|
1786 |
weekOfYear = 1; |
|
1787 |
} |
|
1788 |
} |
|
1789 |
} else { |
|
1790 |
LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone(); |
|
1791 |
long nextJan1; |
|
1792 |
if (jdate.getYear() == 1) { |
|
1793 |
d.addYear(+1); |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1794 |
d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1); |
2 | 1795 |
nextJan1 = jcal.getFixedDate(d); |
1796 |
} else { |
|
1797 |
int nextEraIndex = getEraIndex(d) + 1; |
|
1798 |
CalendarDate cd = eras[nextEraIndex].getSinceDate(); |
|
1799 |
d.setEra(eras[nextEraIndex]); |
|
1800 |
d.setDate(1, cd.getMonth(), cd.getDayOfMonth()); |
|
1801 |
jcal.normalize(d); |
|
1802 |
nextJan1 = jcal.getFixedDate(d); |
|
1803 |
} |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1804 |
long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1805 |
getFirstDayOfWeek()); |
2 | 1806 |
int ndays = (int)(nextJan1st - nextJan1); |
1807 |
if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) { |
|
1808 |
// The first days forms a week in which the date is included. |
|
1809 |
weekOfYear = 1; |
|
1810 |
} |
|
1811 |
} |
|
1812 |
} |
|
1813 |
internalSet(WEEK_OF_YEAR, weekOfYear); |
|
1814 |
internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate)); |
|
1815 |
mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK); |
|
1816 |
} |
|
1817 |
return mask; |
|
1818 |
} |
|
1819 |
||
1820 |
/** |
|
1821 |
* Returns the number of weeks in a period between fixedDay1 and |
|
1822 |
* fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule |
|
1823 |
* is applied to calculate the number of weeks. |
|
1824 |
* |
|
1825 |
* @param fixedDay1 the fixed date of the first day of the period |
|
1826 |
* @param fixedDate the fixed date of the last day of the period |
|
1827 |
* @return the number of weeks of the given period |
|
1828 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1829 |
private int getWeekNumber(long fixedDay1, long fixedDate) { |
2 | 1830 |
// We can always use `jcal' since Julian and Gregorian are the |
1831 |
// same thing for this calculation. |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1832 |
long fixedDay1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDay1 + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
1833 |
getFirstDayOfWeek()); |
2 | 1834 |
int ndays = (int)(fixedDay1st - fixedDay1); |
1835 |
assert ndays <= 7; |
|
1836 |
if (ndays >= getMinimalDaysInFirstWeek()) { |
|
1837 |
fixedDay1st -= 7; |
|
1838 |
} |
|
1839 |
int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st); |
|
1840 |
if (normalizedDayOfPeriod >= 0) { |
|
1841 |
return normalizedDayOfPeriod / 7 + 1; |
|
1842 |
} |
|
1843 |
return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1; |
|
1844 |
} |
|
1845 |
||
1846 |
/** |
|
1847 |
* Converts calendar field values to the time value (millisecond |
|
1848 |
* offset from the <a href="Calendar.html#Epoch">Epoch</a>). |
|
1849 |
* |
|
1850 |
* @exception IllegalArgumentException if any calendar fields are invalid. |
|
1851 |
*/ |
|
1852 |
protected void computeTime() { |
|
1853 |
// In non-lenient mode, perform brief checking of calendar |
|
1854 |
// fields which have been set externally. Through this |
|
1855 |
// checking, the field values are stored in originalFields[] |
|
1856 |
// to see if any of them are normalized later. |
|
1857 |
if (!isLenient()) { |
|
1858 |
if (originalFields == null) { |
|
1859 |
originalFields = new int[FIELD_COUNT]; |
|
1860 |
} |
|
1861 |
for (int field = 0; field < FIELD_COUNT; field++) { |
|
1862 |
int value = internalGet(field); |
|
1863 |
if (isExternallySet(field)) { |
|
1864 |
// Quick validation for any out of range values |
|
1865 |
if (value < getMinimum(field) || value > getMaximum(field)) { |
|
1866 |
throw new IllegalArgumentException(getFieldName(field)); |
|
1867 |
} |
|
1868 |
} |
|
1869 |
originalFields[field] = value; |
|
1870 |
} |
|
1871 |
} |
|
1872 |
||
1873 |
// Let the super class determine which calendar fields to be |
|
1874 |
// used to calculate the time. |
|
1875 |
int fieldMask = selectFields(); |
|
1876 |
||
1877 |
int year; |
|
1878 |
int era; |
|
1879 |
||
1880 |
if (isSet(ERA)) { |
|
1881 |
era = internalGet(ERA); |
|
1882 |
year = isSet(YEAR) ? internalGet(YEAR) : 1; |
|
1883 |
} else { |
|
1884 |
if (isSet(YEAR)) { |
|
1885 |
era = eras.length - 1; |
|
1886 |
year = internalGet(YEAR); |
|
1887 |
} else { |
|
1888 |
// Equivalent to 1970 (Gregorian) |
|
1889 |
era = SHOWA; |
|
1890 |
year = 45; |
|
1891 |
} |
|
1892 |
} |
|
1893 |
||
1894 |
// Calculate the time of day. We rely on the convention that |
|
1895 |
// an UNSET field has 0. |
|
1896 |
long timeOfDay = 0; |
|
1897 |
if (isFieldSet(fieldMask, HOUR_OF_DAY)) { |
|
1898 |
timeOfDay += (long) internalGet(HOUR_OF_DAY); |
|
1899 |
} else { |
|
1900 |
timeOfDay += internalGet(HOUR); |
|
1901 |
// The default value of AM_PM is 0 which designates AM. |
|
1902 |
if (isFieldSet(fieldMask, AM_PM)) { |
|
1903 |
timeOfDay += 12 * internalGet(AM_PM); |
|
1904 |
} |
|
1905 |
} |
|
1906 |
timeOfDay *= 60; |
|
1907 |
timeOfDay += internalGet(MINUTE); |
|
1908 |
timeOfDay *= 60; |
|
1909 |
timeOfDay += internalGet(SECOND); |
|
1910 |
timeOfDay *= 1000; |
|
1911 |
timeOfDay += internalGet(MILLISECOND); |
|
1912 |
||
1913 |
// Convert the time of day to the number of days and the |
|
1914 |
// millisecond offset from midnight. |
|
1915 |
long fixedDate = timeOfDay / ONE_DAY; |
|
1916 |
timeOfDay %= ONE_DAY; |
|
1917 |
while (timeOfDay < 0) { |
|
1918 |
timeOfDay += ONE_DAY; |
|
1919 |
--fixedDate; |
|
1920 |
} |
|
1921 |
||
1922 |
// Calculate the fixed date since January 1, 1 (Gregorian). |
|
1923 |
fixedDate += getFixedDate(era, year, fieldMask); |
|
1924 |
||
1925 |
// millis represents local wall-clock time in milliseconds. |
|
1926 |
long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay; |
|
1927 |
||
1928 |
// Compute the time zone offset and DST offset. There are two potential |
|
1929 |
// ambiguities here. We'll assume a 2:00 am (wall time) switchover time |
|
1930 |
// for discussion purposes here. |
|
1931 |
// 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am |
|
1932 |
// can be in standard or in DST depending. However, 2:00 am is an invalid |
|
1933 |
// representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST). |
|
1934 |
// We assume standard time. |
|
1935 |
// 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am |
|
1936 |
// can be in standard or DST. Both are valid representations (the rep |
|
1937 |
// jumps from 1:59:59 DST to 1:00:00 Std). |
|
1938 |
// Again, we assume standard time. |
|
1939 |
// We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET |
|
1940 |
// or DST_OFFSET fields; then we use those fields. |
|
1941 |
TimeZone zone = getZone(); |
|
1942 |
if (zoneOffsets == null) { |
|
1943 |
zoneOffsets = new int[2]; |
|
1944 |
} |
|
1945 |
int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK); |
|
1946 |
if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) { |
|
1947 |
if (zone instanceof ZoneInfo) { |
|
1948 |
((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets); |
|
1949 |
} else { |
|
1950 |
zone.getOffsets(millis - zone.getRawOffset(), zoneOffsets); |
|
1951 |
} |
|
1952 |
} |
|
1953 |
if (tzMask != 0) { |
|
1954 |
if (isFieldSet(tzMask, ZONE_OFFSET)) { |
|
1955 |
zoneOffsets[0] = internalGet(ZONE_OFFSET); |
|
1956 |
} |
|
1957 |
if (isFieldSet(tzMask, DST_OFFSET)) { |
|
1958 |
zoneOffsets[1] = internalGet(DST_OFFSET); |
|
1959 |
} |
|
1960 |
} |
|
1961 |
||
1962 |
// Adjust the time zone offset values to get the UTC time. |
|
1963 |
millis -= zoneOffsets[0] + zoneOffsets[1]; |
|
1964 |
||
1965 |
// Set this calendar's time in milliseconds |
|
1966 |
time = millis; |
|
1967 |
||
1968 |
int mask = computeFields(fieldMask | getSetStateFields(), tzMask); |
|
1969 |
||
1970 |
if (!isLenient()) { |
|
1971 |
for (int field = 0; field < FIELD_COUNT; field++) { |
|
1972 |
if (!isExternallySet(field)) { |
|
1973 |
continue; |
|
1974 |
} |
|
1975 |
if (originalFields[field] != internalGet(field)) { |
|
1976 |
int wrongValue = internalGet(field); |
|
1977 |
// Restore the original field values |
|
1978 |
System.arraycopy(originalFields, 0, fields, 0, fields.length); |
|
1979 |
throw new IllegalArgumentException(getFieldName(field) + "=" + wrongValue |
|
1980 |
+ ", expected " + originalFields[field]); |
|
1981 |
} |
|
1982 |
} |
|
1983 |
} |
|
1984 |
setFieldsNormalized(mask); |
|
1985 |
} |
|
1986 |
||
1987 |
/** |
|
1988 |
* Computes the fixed date under either the Gregorian or the |
|
1989 |
* Julian calendar, using the given year and the specified calendar fields. |
|
1990 |
* |
|
14014 | 1991 |
* @param era era index |
2 | 1992 |
* @param year the normalized year number, with 0 indicating the |
1993 |
* year 1 BCE, -1 indicating 2 BCE, etc. |
|
1994 |
* @param fieldMask the calendar fields to be used for the date calculation |
|
1995 |
* @return the fixed date |
|
1996 |
* @see Calendar#selectFields |
|
1997 |
*/ |
|
1998 |
private long getFixedDate(int era, int year, int fieldMask) { |
|
1999 |
int month = JANUARY; |
|
2000 |
int firstDayOfMonth = 1; |
|
2001 |
if (isFieldSet(fieldMask, MONTH)) { |
|
2002 |
// No need to check if MONTH has been set (no isSet(MONTH) |
|
2003 |
// call) since its unset value happens to be JANUARY (0). |
|
2004 |
month = internalGet(MONTH); |
|
2005 |
||
2006 |
// If the month is out of range, adjust it into range. |
|
2007 |
if (month > DECEMBER) { |
|
2008 |
year += month / 12; |
|
2009 |
month %= 12; |
|
2010 |
} else if (month < JANUARY) { |
|
2011 |
int[] rem = new int[1]; |
|
2012 |
year += CalendarUtils.floorDivide(month, 12, rem); |
|
2013 |
month = rem[0]; |
|
2014 |
} |
|
2015 |
} else { |
|
2016 |
if (year == 1 && era != 0) { |
|
2017 |
CalendarDate d = eras[era].getSinceDate(); |
|
2018 |
month = d.getMonth() - 1; |
|
2019 |
firstDayOfMonth = d.getDayOfMonth(); |
|
2020 |
} |
|
2021 |
} |
|
2022 |
||
2023 |
// Adjust the base date if year is the minimum value. |
|
2024 |
if (year == MIN_VALUES[YEAR]) { |
|
2025 |
CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
2026 |
int m = dx.getMonth() - 1; |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2027 |
if (month < m) { |
2 | 2028 |
month = m; |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2029 |
} |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2030 |
if (month == m) { |
2 | 2031 |
firstDayOfMonth = dx.getDayOfMonth(); |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2032 |
} |
2 | 2033 |
} |
2034 |
||
2035 |
LocalGregorianCalendar.Date date = jcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
|
2036 |
date.setEra(era > 0 ? eras[era] : null); |
|
2037 |
date.setDate(year, month + 1, firstDayOfMonth); |
|
2038 |
jcal.normalize(date); |
|
2039 |
||
2040 |
// Get the fixed date since Jan 1, 1 (Gregorian). We are on |
|
2041 |
// the first day of either `month' or January in 'year'. |
|
2042 |
long fixedDate = jcal.getFixedDate(date); |
|
2043 |
||
2044 |
if (isFieldSet(fieldMask, MONTH)) { |
|
2045 |
// Month-based calculations |
|
2046 |
if (isFieldSet(fieldMask, DAY_OF_MONTH)) { |
|
2047 |
// We are on the "first day" of the month (which may |
|
2048 |
// not be 1). Just add the offset if DAY_OF_MONTH is |
|
2049 |
// set. If the isSet call returns false, that means |
|
2050 |
// DAY_OF_MONTH has been selected just because of the |
|
2051 |
// selected combination. We don't need to add any |
|
2052 |
// since the default value is the "first day". |
|
2053 |
if (isSet(DAY_OF_MONTH)) { |
|
2054 |
// To avoid underflow with DAY_OF_MONTH-firstDayOfMonth, add |
|
2055 |
// DAY_OF_MONTH, then subtract firstDayOfMonth. |
|
2056 |
fixedDate += internalGet(DAY_OF_MONTH); |
|
2057 |
fixedDate -= firstDayOfMonth; |
|
2058 |
} |
|
2059 |
} else { |
|
2060 |
if (isFieldSet(fieldMask, WEEK_OF_MONTH)) { |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2061 |
long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2062 |
getFirstDayOfWeek()); |
2 | 2063 |
// If we have enough days in the first week, then |
2064 |
// move to the previous week. |
|
2065 |
if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) { |
|
2066 |
firstDayOfWeek -= 7; |
|
2067 |
} |
|
2068 |
if (isFieldSet(fieldMask, DAY_OF_WEEK)) { |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2069 |
firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2070 |
internalGet(DAY_OF_WEEK)); |
2 | 2071 |
} |
2072 |
// In lenient mode, we treat days of the previous |
|
2073 |
// months as a part of the specified |
|
2074 |
// WEEK_OF_MONTH. See 4633646. |
|
2075 |
fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1); |
|
2076 |
} else { |
|
2077 |
int dayOfWeek; |
|
2078 |
if (isFieldSet(fieldMask, DAY_OF_WEEK)) { |
|
2079 |
dayOfWeek = internalGet(DAY_OF_WEEK); |
|
2080 |
} else { |
|
2081 |
dayOfWeek = getFirstDayOfWeek(); |
|
2082 |
} |
|
2083 |
// We are basing this on the day-of-week-in-month. The only |
|
2084 |
// trickiness occurs if the day-of-week-in-month is |
|
2085 |
// negative. |
|
2086 |
int dowim; |
|
2087 |
if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) { |
|
2088 |
dowim = internalGet(DAY_OF_WEEK_IN_MONTH); |
|
2089 |
} else { |
|
2090 |
dowim = 1; |
|
2091 |
} |
|
2092 |
if (dowim >= 0) { |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2093 |
fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2094 |
dayOfWeek); |
2 | 2095 |
} else { |
2096 |
// Go to the first day of the next week of |
|
2097 |
// the specified week boundary. |
|
2098 |
int lastDate = monthLength(month, year) + (7 * (dowim + 1)); |
|
2099 |
// Then, get the day of week date on or before the last date. |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2100 |
fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2101 |
dayOfWeek); |
2 | 2102 |
} |
2103 |
} |
|
2104 |
} |
|
2105 |
} else { |
|
2106 |
// We are on the first day of the year. |
|
2107 |
if (isFieldSet(fieldMask, DAY_OF_YEAR)) { |
|
2108 |
if (isTransitionYear(date.getNormalizedYear())) { |
|
2109 |
fixedDate = getFixedDateJan1(date, fixedDate); |
|
2110 |
} |
|
2111 |
// Add the offset, then subtract 1. (Make sure to avoid underflow.) |
|
2112 |
fixedDate += internalGet(DAY_OF_YEAR); |
|
2113 |
fixedDate--; |
|
2114 |
} else { |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2115 |
long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2116 |
getFirstDayOfWeek()); |
2 | 2117 |
// If we have enough days in the first week, then move |
2118 |
// to the previous week. |
|
2119 |
if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) { |
|
2120 |
firstDayOfWeek -= 7; |
|
2121 |
} |
|
2122 |
if (isFieldSet(fieldMask, DAY_OF_WEEK)) { |
|
2123 |
int dayOfWeek = internalGet(DAY_OF_WEEK); |
|
2124 |
if (dayOfWeek != getFirstDayOfWeek()) { |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2125 |
firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2126 |
dayOfWeek); |
2 | 2127 |
} |
2128 |
} |
|
2129 |
fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1); |
|
2130 |
} |
|
2131 |
} |
|
2132 |
return fixedDate; |
|
2133 |
} |
|
2134 |
||
2135 |
/** |
|
2136 |
* Returns the fixed date of the first day of the year (usually |
|
2137 |
* January 1) before the specified date. |
|
2138 |
* |
|
2139 |
* @param date the date for which the first day of the year is |
|
2140 |
* calculated. The date has to be in the cut-over year. |
|
2141 |
* @param fixedDate the fixed date representation of the date |
|
2142 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2143 |
private long getFixedDateJan1(LocalGregorianCalendar.Date date, long fixedDate) { |
2 | 2144 |
Era era = date.getEra(); |
2145 |
if (date.getEra() != null && date.getYear() == 1) { |
|
2146 |
for (int eraIndex = getEraIndex(date); eraIndex > 0; eraIndex--) { |
|
2147 |
CalendarDate d = eras[eraIndex].getSinceDate(); |
|
2148 |
long fd = gcal.getFixedDate(d); |
|
2149 |
// There might be multiple era transitions in a year. |
|
2150 |
if (fd > fixedDate) { |
|
2151 |
continue; |
|
2152 |
} |
|
2153 |
return fd; |
|
2154 |
} |
|
2155 |
} |
|
2156 |
CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2157 |
d.setDate(date.getNormalizedYear(), Gregorian.JANUARY, 1); |
2 | 2158 |
return gcal.getFixedDate(d); |
2159 |
} |
|
2160 |
||
2161 |
/** |
|
2162 |
* Returns the fixed date of the first date of the month (usually |
|
2163 |
* the 1st of the month) before the specified date. |
|
2164 |
* |
|
2165 |
* @param date the date for which the first day of the month is |
|
2166 |
* calculated. The date must be in the era transition year. |
|
2167 |
* @param fixedDate the fixed date representation of the date |
|
2168 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2169 |
private long getFixedDateMonth1(LocalGregorianCalendar.Date date, |
2 | 2170 |
long fixedDate) { |
2171 |
int eraIndex = getTransitionEraIndex(date); |
|
2172 |
if (eraIndex != -1) { |
|
2173 |
long transition = sinceFixedDates[eraIndex]; |
|
2174 |
// If the given date is on or after the transition date, then |
|
2175 |
// return the transition date. |
|
2176 |
if (transition <= fixedDate) { |
|
2177 |
return transition; |
|
2178 |
} |
|
2179 |
} |
|
2180 |
||
2181 |
// Otherwise, we can use the 1st day of the month. |
|
2182 |
return fixedDate - date.getDayOfMonth() + 1; |
|
2183 |
} |
|
2184 |
||
2185 |
/** |
|
2186 |
* Returns a LocalGregorianCalendar.Date produced from the specified fixed date. |
|
2187 |
* |
|
2188 |
* @param fd the fixed date |
|
2189 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2190 |
private static LocalGregorianCalendar.Date getCalendarDate(long fd) { |
2 | 2191 |
LocalGregorianCalendar.Date d = jcal.newCalendarDate(TimeZone.NO_TIMEZONE); |
2192 |
jcal.getCalendarDateFromFixedDate(d, fd); |
|
2193 |
return d; |
|
2194 |
} |
|
2195 |
||
2196 |
/** |
|
2197 |
* Returns the length of the specified month in the specified |
|
2198 |
* Gregorian year. The year number must be normalized. |
|
2199 |
* |
|
14014 | 2200 |
* @see GregorianCalendar#isLeapYear(int) |
2 | 2201 |
*/ |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2202 |
private int monthLength(int month, int gregorianYear) { |
2 | 2203 |
return CalendarUtils.isGregorianLeapYear(gregorianYear) ? |
2204 |
GregorianCalendar.LEAP_MONTH_LENGTH[month] : GregorianCalendar.MONTH_LENGTH[month]; |
|
2205 |
} |
|
2206 |
||
2207 |
/** |
|
2208 |
* Returns the length of the specified month in the year provided |
|
2209 |
* by internalGet(YEAR). |
|
2210 |
* |
|
14014 | 2211 |
* @see GregorianCalendar#isLeapYear(int) |
2 | 2212 |
*/ |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2213 |
private int monthLength(int month) { |
2 | 2214 |
assert jdate.isNormalized(); |
2215 |
return jdate.isLeapYear() ? |
|
2216 |
GregorianCalendar.LEAP_MONTH_LENGTH[month] : GregorianCalendar.MONTH_LENGTH[month]; |
|
2217 |
} |
|
2218 |
||
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2219 |
private int actualMonthLength() { |
2 | 2220 |
int length = jcal.getMonthLength(jdate); |
2221 |
int eraIndex = getTransitionEraIndex(jdate); |
|
2222 |
if (eraIndex == -1) { |
|
2223 |
long transitionFixedDate = sinceFixedDates[eraIndex]; |
|
2224 |
CalendarDate d = eras[eraIndex].getSinceDate(); |
|
2225 |
if (transitionFixedDate <= cachedFixedDate) { |
|
2226 |
length -= d.getDayOfMonth() - 1; |
|
2227 |
} else { |
|
2228 |
length = d.getDayOfMonth() - 1; |
|
2229 |
} |
|
2230 |
} |
|
2231 |
return length; |
|
2232 |
} |
|
2233 |
||
2234 |
/** |
|
2235 |
* Returns the index to the new era if the given date is in a |
|
2236 |
* transition month. For example, if the give date is Heisei 1 |
|
2237 |
* (1989) January 20, then the era index for Heisei is |
|
2238 |
* returned. Likewise, if the given date is Showa 64 (1989) |
|
2239 |
* January 3, then the era index for Heisei is returned. If the |
|
2240 |
* given date is not in any transition month, then -1 is returned. |
|
2241 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2242 |
private static int getTransitionEraIndex(LocalGregorianCalendar.Date date) { |
2 | 2243 |
int eraIndex = getEraIndex(date); |
2244 |
CalendarDate transitionDate = eras[eraIndex].getSinceDate(); |
|
2245 |
if (transitionDate.getYear() == date.getNormalizedYear() && |
|
2246 |
transitionDate.getMonth() == date.getMonth()) { |
|
2247 |
return eraIndex; |
|
2248 |
} |
|
2249 |
if (eraIndex < eras.length - 1) { |
|
2250 |
transitionDate = eras[++eraIndex].getSinceDate(); |
|
2251 |
if (transitionDate.getYear() == date.getNormalizedYear() && |
|
2252 |
transitionDate.getMonth() == date.getMonth()) { |
|
2253 |
return eraIndex; |
|
2254 |
} |
|
2255 |
} |
|
2256 |
return -1; |
|
2257 |
} |
|
2258 |
||
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2259 |
private boolean isTransitionYear(int normalizedYear) { |
2 | 2260 |
for (int i = eras.length - 1; i > 0; i--) { |
2261 |
int transitionYear = eras[i].getSinceDate().getYear(); |
|
2262 |
if (normalizedYear == transitionYear) { |
|
2263 |
return true; |
|
2264 |
} |
|
2265 |
if (normalizedYear > transitionYear) { |
|
2266 |
break; |
|
2267 |
} |
|
2268 |
} |
|
2269 |
return false; |
|
2270 |
} |
|
2271 |
||
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2272 |
private static int getEraIndex(LocalGregorianCalendar.Date date) { |
2 | 2273 |
Era era = date.getEra(); |
2274 |
for (int i = eras.length - 1; i > 0; i--) { |
|
2275 |
if (eras[i] == era) { |
|
2276 |
return i; |
|
2277 |
} |
|
2278 |
} |
|
2279 |
return 0; |
|
2280 |
} |
|
2281 |
||
2282 |
/** |
|
2283 |
* Returns this object if it's normalized (all fields and time are |
|
2284 |
* in sync). Otherwise, a cloned object is returned after calling |
|
2285 |
* complete() in lenient mode. |
|
2286 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2287 |
private JapaneseImperialCalendar getNormalizedCalendar() { |
2 | 2288 |
JapaneseImperialCalendar jc; |
2289 |
if (isFullyNormalized()) { |
|
2290 |
jc = this; |
|
2291 |
} else { |
|
2292 |
// Create a clone and normalize the calendar fields |
|
2293 |
jc = (JapaneseImperialCalendar) this.clone(); |
|
2294 |
jc.setLenient(true); |
|
2295 |
jc.complete(); |
|
2296 |
} |
|
2297 |
return jc; |
|
2298 |
} |
|
2299 |
||
2300 |
/** |
|
2301 |
* After adjustments such as add(MONTH), add(YEAR), we don't want the |
|
2302 |
* month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar |
|
2303 |
* 3, we want it to go to Feb 28. Adjustments which might run into this |
|
2304 |
* problem call this method to retain the proper month. |
|
2305 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2306 |
private void pinDayOfMonth(LocalGregorianCalendar.Date date) { |
2 | 2307 |
int year = date.getYear(); |
2308 |
int dom = date.getDayOfMonth(); |
|
2309 |
if (year != getMinimum(YEAR)) { |
|
2310 |
date.setDayOfMonth(1); |
|
2311 |
jcal.normalize(date); |
|
2312 |
int monthLength = jcal.getMonthLength(date); |
|
2313 |
if (dom > monthLength) { |
|
2314 |
date.setDayOfMonth(monthLength); |
|
2315 |
} else { |
|
2316 |
date.setDayOfMonth(dom); |
|
2317 |
} |
|
2318 |
jcal.normalize(date); |
|
2319 |
} else { |
|
2320 |
LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); |
|
2321 |
LocalGregorianCalendar.Date realDate = jcal.getCalendarDate(time, getZone()); |
|
2322 |
long tod = realDate.getTimeOfDay(); |
|
2323 |
// Use an equivalent year. |
|
2324 |
realDate.addYear(+400); |
|
2325 |
realDate.setMonth(date.getMonth()); |
|
2326 |
realDate.setDayOfMonth(1); |
|
2327 |
jcal.normalize(realDate); |
|
2328 |
int monthLength = jcal.getMonthLength(realDate); |
|
2329 |
if (dom > monthLength) { |
|
2330 |
realDate.setDayOfMonth(monthLength); |
|
2331 |
} else { |
|
2332 |
if (dom < d.getDayOfMonth()) { |
|
2333 |
realDate.setDayOfMonth(d.getDayOfMonth()); |
|
2334 |
} else { |
|
2335 |
realDate.setDayOfMonth(dom); |
|
2336 |
} |
|
2337 |
} |
|
2338 |
if (realDate.getDayOfMonth() == d.getDayOfMonth() && tod < d.getTimeOfDay()) { |
|
2339 |
realDate.setDayOfMonth(Math.min(dom + 1, monthLength)); |
|
2340 |
} |
|
2341 |
// restore the year. |
|
2342 |
date.setDate(year, realDate.getMonth(), realDate.getDayOfMonth()); |
|
2343 |
// Don't normalize date here so as not to cause underflow. |
|
2344 |
} |
|
2345 |
} |
|
2346 |
||
2347 |
/** |
|
2348 |
* Returns the new value after 'roll'ing the specified value and amount. |
|
2349 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2350 |
private static int getRolledValue(int value, int amount, int min, int max) { |
2 | 2351 |
assert value >= min && value <= max; |
2352 |
int range = max - min + 1; |
|
2353 |
amount %= range; |
|
2354 |
int n = value + amount; |
|
2355 |
if (n > max) { |
|
2356 |
n -= range; |
|
2357 |
} else if (n < min) { |
|
2358 |
n += range; |
|
2359 |
} |
|
2360 |
assert n >= min && n <= max; |
|
2361 |
return n; |
|
2362 |
} |
|
2363 |
||
2364 |
/** |
|
2365 |
* Returns the ERA. We need a special method for this because the |
|
2366 |
* default ERA is the current era, but a zero (unset) ERA means before Meiji. |
|
2367 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
5506
diff
changeset
|
2368 |
private int internalGetEra() { |
2 | 2369 |
return isSet(ERA) ? internalGet(ERA) : eras.length - 1; |
2370 |
} |
|
2371 |
||
2372 |
/** |
|
2373 |
* Updates internal state. |
|
2374 |
*/ |
|
2375 |
private void readObject(ObjectInputStream stream) |
|
2376 |
throws IOException, ClassNotFoundException { |
|
2377 |
stream.defaultReadObject(); |
|
2378 |
if (jdate == null) { |
|
2379 |
jdate = jcal.newCalendarDate(getZone()); |
|
2380 |
cachedFixedDate = Long.MIN_VALUE; |
|
2381 |
} |
|
2382 |
} |
|
2383 |
} |