author | rriggs |
Wed, 11 Sep 2013 10:16:21 -0400 | |
changeset 19841 | 15c8e97d6a14 |
parent 19030 | 32f129cb6351 |
child 20518 | dde564773845 |
permissions | -rw-r--r-- |
15658 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos |
|
28 |
* |
|
29 |
* All rights reserved. |
|
30 |
* |
|
31 |
* Redistribution and use in source and binary forms, with or without |
|
32 |
* modification, are permitted provided that the following conditions are met: |
|
33 |
* |
|
34 |
* * Redistributions of source code must retain the above copyright notice, |
|
35 |
* this list of conditions and the following disclaimer. |
|
36 |
* |
|
37 |
* * Redistributions in binary form must reproduce the above copyright notice, |
|
38 |
* this list of conditions and the following disclaimer in the documentation |
|
39 |
* and/or other materials provided with the distribution. |
|
40 |
* |
|
41 |
* * Neither the name of JSR-310 nor the names of its contributors |
|
42 |
* may be used to endorse or promote products derived from this software |
|
43 |
* without specific prior written permission. |
|
44 |
* |
|
45 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
46 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
47 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
48 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
49 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
50 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
51 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
52 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
53 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
54 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
55 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
56 |
*/ |
|
57 |
package java.time.chrono; |
|
58 |
||
19030 | 59 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; |
60 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; |
|
61 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; |
|
62 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; |
|
15658 | 63 |
import static java.time.temporal.ChronoField.DAY_OF_MONTH; |
19030 | 64 |
import static java.time.temporal.ChronoField.DAY_OF_YEAR; |
15658 | 65 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
66 |
import static java.time.temporal.ChronoField.YEAR; |
|
19030 | 67 |
import static java.time.temporal.ChronoField.YEAR_OF_ERA; |
15658 | 68 |
|
69 |
import java.io.DataInput; |
|
70 |
import java.io.DataOutput; |
|
71 |
import java.io.IOException; |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
72 |
import java.io.InvalidObjectException; |
15658 | 73 |
import java.io.Serializable; |
74 |
import java.time.Clock; |
|
75 |
import java.time.DateTimeException; |
|
76 |
import java.time.LocalDate; |
|
77 |
import java.time.LocalTime; |
|
78 |
import java.time.Period; |
|
16852 | 79 |
import java.time.Year; |
15658 | 80 |
import java.time.ZoneId; |
81 |
import java.time.temporal.ChronoField; |
|
82 |
import java.time.temporal.TemporalAccessor; |
|
83 |
import java.time.temporal.TemporalAdjuster; |
|
84 |
import java.time.temporal.TemporalAmount; |
|
85 |
import java.time.temporal.TemporalField; |
|
16852 | 86 |
import java.time.temporal.TemporalQuery; |
15658 | 87 |
import java.time.temporal.TemporalUnit; |
16852 | 88 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 89 |
import java.time.temporal.ValueRange; |
90 |
import java.util.Calendar; |
|
91 |
import java.util.Objects; |
|
92 |
||
17474 | 93 |
import sun.util.calendar.CalendarDate; |
15658 | 94 |
import sun.util.calendar.LocalGregorianCalendar; |
95 |
||
96 |
/** |
|
97 |
* A date in the Japanese Imperial calendar system. |
|
98 |
* <p> |
|
99 |
* This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}. |
|
100 |
* This calendar system is primarily used in Japan. |
|
101 |
* <p> |
|
102 |
* The Japanese Imperial calendar system is the same as the ISO calendar system |
|
103 |
* apart from the era-based year numbering. The proleptic-year is defined to be |
|
104 |
* equal to the ISO proleptic-year. |
|
105 |
* <p> |
|
19030 | 106 |
* Japan introduced the Gregorian calendar starting with Meiji 6. |
107 |
* Only Meiji and later eras are supported; |
|
108 |
* dates before Meiji 6, January 1 are not supported. |
|
109 |
* <p> |
|
15658 | 110 |
* For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br> |
111 |
* Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br> |
|
112 |
* Calling {@code japaneseDate.get(YEAR)} will return 2012.<br> |
|
113 |
* Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to |
|
114 |
* {@code JapaneseChronology.ERA_HEISEI}.<br> |
|
115 |
* |
|
17474 | 116 |
* @implSpec |
15658 | 117 |
* This class is immutable and thread-safe. |
118 |
* |
|
119 |
* @since 1.8 |
|
120 |
*/ |
|
121 |
public final class JapaneseDate |
|
122 |
extends ChronoDateImpl<JapaneseDate> |
|
19030 | 123 |
implements ChronoLocalDate, Serializable { |
15658 | 124 |
|
125 |
/** |
|
126 |
* Serialization version. |
|
127 |
*/ |
|
128 |
private static final long serialVersionUID = -305327627230580483L; |
|
129 |
||
130 |
/** |
|
131 |
* The underlying ISO local date. |
|
132 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
133 |
private final transient LocalDate isoDate; |
15658 | 134 |
/** |
135 |
* The JapaneseEra of this date. |
|
136 |
*/ |
|
137 |
private transient JapaneseEra era; |
|
138 |
/** |
|
139 |
* The Japanese imperial calendar year of this date. |
|
140 |
*/ |
|
141 |
private transient int yearOfEra; |
|
142 |
||
19030 | 143 |
/** |
144 |
* The first day supported by the JapaneseChronology is Meiji 6, January 1st. |
|
145 |
*/ |
|
146 |
final static LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1); |
|
147 |
||
15658 | 148 |
//----------------------------------------------------------------------- |
149 |
/** |
|
150 |
* Obtains the current {@code JapaneseDate} from the system clock in the default time-zone. |
|
151 |
* <p> |
|
152 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default |
|
153 |
* time-zone to obtain the current date. |
|
154 |
* <p> |
|
155 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
156 |
* because the clock is hard-coded. |
|
157 |
* |
|
158 |
* @return the current date using the system clock and default time-zone, not null |
|
159 |
*/ |
|
160 |
public static JapaneseDate now() { |
|
161 |
return now(Clock.systemDefaultZone()); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Obtains the current {@code JapaneseDate} from the system clock in the specified time-zone. |
|
166 |
* <p> |
|
167 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. |
|
168 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
169 |
* <p> |
|
170 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
171 |
* because the clock is hard-coded. |
|
172 |
* |
|
173 |
* @param zone the zone ID to use, not null |
|
174 |
* @return the current date using the system clock, not null |
|
175 |
*/ |
|
176 |
public static JapaneseDate now(ZoneId zone) { |
|
177 |
return now(Clock.system(zone)); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Obtains the current {@code JapaneseDate} from the specified clock. |
|
182 |
* <p> |
|
183 |
* This will query the specified clock to obtain the current date - today. |
|
184 |
* Using this method allows the use of an alternate clock for testing. |
|
185 |
* The alternate clock may be introduced using {@linkplain Clock dependency injection}. |
|
186 |
* |
|
187 |
* @param clock the clock to use, not null |
|
188 |
* @return the current date, not null |
|
189 |
* @throws DateTimeException if the current date cannot be obtained |
|
190 |
*/ |
|
191 |
public static JapaneseDate now(Clock clock) { |
|
19030 | 192 |
return new JapaneseDate(LocalDate.now(clock)); |
15658 | 193 |
} |
194 |
||
195 |
/** |
|
196 |
* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar |
|
197 |
* system from the era, year-of-era, month-of-year and day-of-month fields. |
|
198 |
* <p> |
|
199 |
* This returns a {@code JapaneseDate} with the specified fields. |
|
200 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
|
19030 | 201 |
* <p> |
202 |
* The Japanese month and day-of-month are the same as those in the |
|
203 |
* ISO calendar system. They are not reset when the era changes. |
|
204 |
* For example: |
|
205 |
* <pre> |
|
206 |
* 6th Jan Showa 64 = ISO 1989-01-06 |
|
207 |
* 7th Jan Showa 64 = ISO 1989-01-07 |
|
208 |
* 8th Jan Heisei 1 = ISO 1989-01-08 |
|
209 |
* 9th Jan Heisei 1 = ISO 1989-01-09 |
|
210 |
* </pre> |
|
15658 | 211 |
* |
212 |
* @param era the Japanese era, not null |
|
213 |
* @param yearOfEra the Japanese year-of-era |
|
214 |
* @param month the Japanese month-of-year, from 1 to 12 |
|
215 |
* @param dayOfMonth the Japanese day-of-month, from 1 to 31 |
|
216 |
* @return the date in Japanese calendar system, not null |
|
217 |
* @throws DateTimeException if the value of any field is out of range, |
|
218 |
* or if the day-of-month is invalid for the month-year, |
|
219 |
* or if the date is not a Japanese era |
|
220 |
*/ |
|
19030 | 221 |
public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) { |
222 |
Objects.requireNonNull(era, "era"); |
|
223 |
LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null); |
|
224 |
jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth); |
|
225 |
if (!JapaneseChronology.JCAL.validate(jdate)) { |
|
226 |
throw new DateTimeException("year, month, and day not valid for Era"); |
|
15658 | 227 |
} |
19030 | 228 |
LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth); |
229 |
return new JapaneseDate(era, yearOfEra, date); |
|
15658 | 230 |
} |
231 |
||
232 |
/** |
|
233 |
* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar |
|
234 |
* system from the proleptic-year, month-of-year and day-of-month fields. |
|
235 |
* <p> |
|
236 |
* This returns a {@code JapaneseDate} with the specified fields. |
|
237 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
|
19030 | 238 |
* <p> |
239 |
* The Japanese proleptic year, month and day-of-month are the same as those |
|
240 |
* in the ISO calendar system. They are not reset when the era changes. |
|
15658 | 241 |
* |
242 |
* @param prolepticYear the Japanese proleptic-year |
|
243 |
* @param month the Japanese month-of-year, from 1 to 12 |
|
244 |
* @param dayOfMonth the Japanese day-of-month, from 1 to 31 |
|
245 |
* @return the date in Japanese calendar system, not null |
|
246 |
* @throws DateTimeException if the value of any field is out of range, |
|
247 |
* or if the day-of-month is invalid for the month-year |
|
248 |
*/ |
|
249 |
public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) { |
|
250 |
return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth)); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar |
|
19030 | 255 |
* system from the era, year-of-era and day-of-year fields. |
15658 | 256 |
* <p> |
257 |
* This returns a {@code JapaneseDate} with the specified fields. |
|
258 |
* The day must be valid for the year, otherwise an exception will be thrown. |
|
19030 | 259 |
* <p> |
260 |
* The day-of-year in this factory is expressed relative to the start of the year-of-era. |
|
261 |
* This definition changes the normal meaning of day-of-year only in those years |
|
262 |
* where the year-of-era is reset to one due to a change in the era. |
|
263 |
* For example: |
|
264 |
* <pre> |
|
265 |
* 6th Jan Showa 64 = day-of-year 6 |
|
266 |
* 7th Jan Showa 64 = day-of-year 7 |
|
267 |
* 8th Jan Heisei 1 = day-of-year 1 |
|
268 |
* 9th Jan Heisei 1 = day-of-year 2 |
|
269 |
* </pre> |
|
15658 | 270 |
* |
19030 | 271 |
* @param era the Japanese era, not null |
272 |
* @param yearOfEra the Japanese year-of-era |
|
15658 | 273 |
* @param dayOfYear the chronology day-of-year, from 1 to 366 |
274 |
* @return the date in Japanese calendar system, not null |
|
275 |
* @throws DateTimeException if the value of any field is out of range, |
|
276 |
* or if the day-of-year is invalid for the year |
|
277 |
*/ |
|
17474 | 278 |
static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) { |
19030 | 279 |
Objects.requireNonNull(era, "era"); |
17474 | 280 |
CalendarDate firstDay = era.getPrivateEra().getSinceDate(); |
281 |
LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null); |
|
282 |
jdate.setEra(era.getPrivateEra()); |
|
283 |
if (yearOfEra == 1) { |
|
284 |
jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1); |
|
285 |
} else { |
|
286 |
jdate.setDate(yearOfEra, 1, dayOfYear); |
|
287 |
} |
|
288 |
JapaneseChronology.JCAL.normalize(jdate); |
|
289 |
if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) { |
|
290 |
throw new DateTimeException("Invalid parameters"); |
|
291 |
} |
|
292 |
LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(), |
|
293 |
jdate.getMonth(), jdate.getDayOfMonth()); |
|
294 |
return new JapaneseDate(era, yearOfEra, localdate); |
|
295 |
} |
|
296 |
||
15658 | 297 |
/** |
298 |
* Obtains a {@code JapaneseDate} from a temporal object. |
|
299 |
* <p> |
|
300 |
* This obtains a date in the Japanese calendar system based on the specified temporal. |
|
301 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
302 |
* which this factory converts to an instance of {@code JapaneseDate}. |
|
303 |
* <p> |
|
304 |
* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} |
|
305 |
* field, which is standardized across calendar systems. |
|
306 |
* <p> |
|
307 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
308 |
* allowing it to be used as a query via method reference, {@code JapaneseDate::from}. |
|
309 |
* |
|
310 |
* @param temporal the temporal object to convert, not null |
|
311 |
* @return the date in Japanese calendar system, not null |
|
312 |
* @throws DateTimeException if unable to convert to a {@code JapaneseDate} |
|
313 |
*/ |
|
314 |
public static JapaneseDate from(TemporalAccessor temporal) { |
|
315 |
return JapaneseChronology.INSTANCE.date(temporal); |
|
316 |
} |
|
317 |
||
318 |
//----------------------------------------------------------------------- |
|
319 |
/** |
|
320 |
* Creates an instance from an ISO date. |
|
321 |
* |
|
322 |
* @param isoDate the standard local date, validated not null |
|
323 |
*/ |
|
324 |
JapaneseDate(LocalDate isoDate) { |
|
19030 | 325 |
if (isoDate.isBefore(MEIJI_6_ISODATE)) { |
326 |
throw new DateTimeException("JapaneseDate before Meiji 6 is not supported"); |
|
327 |
} |
|
15658 | 328 |
LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate); |
329 |
this.era = JapaneseEra.toJapaneseEra(jdate.getEra()); |
|
330 |
this.yearOfEra = jdate.getYear(); |
|
331 |
this.isoDate = isoDate; |
|
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters, |
|
336 |
* and {@code era} and {@code year} must agree with {@code isoDate}. |
|
337 |
* |
|
338 |
* @param era the era, validated not null |
|
339 |
* @param year the year-of-era, validated |
|
340 |
* @param isoDate the standard local date, validated not null |
|
341 |
*/ |
|
342 |
JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) { |
|
19030 | 343 |
if (isoDate.isBefore(MEIJI_6_ISODATE)) { |
344 |
throw new DateTimeException("JapaneseDate before Meiji 6 is not supported"); |
|
345 |
} |
|
15658 | 346 |
this.era = era; |
347 |
this.yearOfEra = year; |
|
348 |
this.isoDate = isoDate; |
|
349 |
} |
|
350 |
||
351 |
//----------------------------------------------------------------------- |
|
16852 | 352 |
/** |
353 |
* Gets the chronology of this date, which is the Japanese calendar system. |
|
354 |
* <p> |
|
355 |
* The {@code Chronology} represents the calendar system in use. |
|
356 |
* The era and other fields in {@link ChronoField} are defined by the chronology. |
|
357 |
* |
|
358 |
* @return the Japanese chronology, not null |
|
359 |
*/ |
|
15658 | 360 |
@Override |
361 |
public JapaneseChronology getChronology() { |
|
362 |
return JapaneseChronology.INSTANCE; |
|
363 |
} |
|
364 |
||
16852 | 365 |
/** |
366 |
* Gets the era applicable at this date. |
|
367 |
* <p> |
|
368 |
* The Japanese calendar system has multiple eras defined by {@link JapaneseEra}. |
|
369 |
* |
|
370 |
* @return the era applicable at this date, not null |
|
371 |
*/ |
|
372 |
@Override |
|
373 |
public JapaneseEra getEra() { |
|
374 |
return era; |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Returns the length of the month represented by this date. |
|
379 |
* <p> |
|
380 |
* This returns the length of the month in days. |
|
381 |
* Month lengths match those of the ISO calendar system. |
|
382 |
* |
|
383 |
* @return the length of the month in days |
|
384 |
*/ |
|
15658 | 385 |
@Override |
386 |
public int lengthOfMonth() { |
|
387 |
return isoDate.lengthOfMonth(); |
|
388 |
} |
|
389 |
||
19030 | 390 |
@Override |
391 |
public int lengthOfYear() { |
|
392 |
Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); |
|
393 |
jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); |
|
394 |
jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); |
|
395 |
return jcal.getActualMaximum(Calendar.DAY_OF_YEAR); |
|
396 |
} |
|
397 |
||
16852 | 398 |
//----------------------------------------------------------------------- |
19030 | 399 |
/** |
400 |
* Checks if the specified field is supported. |
|
401 |
* <p> |
|
402 |
* This checks if this date can be queried for the specified field. |
|
403 |
* If false, then calling the {@link #range(TemporalField) range} and |
|
404 |
* {@link #get(TemporalField) get} methods will throw an exception. |
|
405 |
* <p> |
|
406 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
407 |
* The supported fields are: |
|
408 |
* <ul> |
|
409 |
* <li>{@code DAY_OF_WEEK} |
|
410 |
* <li>{@code DAY_OF_MONTH} |
|
411 |
* <li>{@code DAY_OF_YEAR} |
|
412 |
* <li>{@code EPOCH_DAY} |
|
413 |
* <li>{@code MONTH_OF_YEAR} |
|
414 |
* <li>{@code PROLEPTIC_MONTH} |
|
415 |
* <li>{@code YEAR_OF_ERA} |
|
416 |
* <li>{@code YEAR} |
|
417 |
* <li>{@code ERA} |
|
418 |
* </ul> |
|
419 |
* All other {@code ChronoField} instances will return false. |
|
420 |
* <p> |
|
421 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
422 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
|
423 |
* passing {@code this} as the argument. |
|
424 |
* Whether the field is supported is determined by the field. |
|
425 |
* |
|
426 |
* @param field the field to check, null returns false |
|
427 |
* @return true if the field is supported on this date, false if not |
|
428 |
*/ |
|
429 |
@Override |
|
430 |
public boolean isSupported(TemporalField field) { |
|
431 |
if (field == ALIGNED_DAY_OF_WEEK_IN_MONTH || field == ALIGNED_DAY_OF_WEEK_IN_YEAR || |
|
432 |
field == ALIGNED_WEEK_OF_MONTH || field == ALIGNED_WEEK_OF_YEAR) { |
|
433 |
return false; |
|
434 |
} |
|
435 |
return ChronoLocalDate.super.isSupported(field); |
|
436 |
} |
|
437 |
||
15658 | 438 |
@Override |
439 |
public ValueRange range(TemporalField field) { |
|
440 |
if (field instanceof ChronoField) { |
|
441 |
if (isSupported(field)) { |
|
442 |
ChronoField f = (ChronoField) field; |
|
443 |
switch (f) { |
|
19030 | 444 |
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); |
445 |
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); |
|
446 |
case YEAR_OF_ERA: { |
|
447 |
Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); |
|
448 |
jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); |
|
449 |
jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); |
|
450 |
return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR)); |
|
451 |
} |
|
15658 | 452 |
} |
453 |
return getChronology().range(f); |
|
454 |
} |
|
19030 | 455 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 456 |
} |
457 |
return field.rangeRefinedBy(this); |
|
458 |
} |
|
459 |
||
460 |
@Override |
|
461 |
public long getLong(TemporalField field) { |
|
462 |
if (field instanceof ChronoField) { |
|
16852 | 463 |
// same as ISO: |
19030 | 464 |
// DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR |
16852 | 465 |
// |
466 |
// calendar specific fields |
|
19030 | 467 |
// DAY_OF_YEAR, YEAR_OF_ERA, ERA |
15658 | 468 |
switch ((ChronoField) field) { |
19030 | 469 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: |
470 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: |
|
471 |
case ALIGNED_WEEK_OF_MONTH: |
|
472 |
case ALIGNED_WEEK_OF_YEAR: |
|
473 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
|
15658 | 474 |
case YEAR_OF_ERA: |
475 |
return yearOfEra; |
|
476 |
case ERA: |
|
477 |
return era.getValue(); |
|
19030 | 478 |
case DAY_OF_YEAR: |
479 |
Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); |
|
480 |
jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); |
|
481 |
jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); |
|
482 |
return jcal.get(Calendar.DAY_OF_YEAR); |
|
15658 | 483 |
} |
484 |
return isoDate.getLong(field); |
|
485 |
} |
|
486 |
return field.getFrom(this); |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}. |
|
491 |
* |
|
492 |
* @param isoDate the local date, not null |
|
493 |
* @return a {@code LocalGregorianCalendar.Date}, not null |
|
494 |
*/ |
|
495 |
private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) { |
|
496 |
LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null); |
|
497 |
sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate); |
|
498 |
int year = isoDate.getYear(); |
|
499 |
if (sunEra != null) { |
|
500 |
year -= sunEra.getSinceDate().getYear() - 1; |
|
501 |
} |
|
502 |
jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth()); |
|
503 |
JapaneseChronology.JCAL.normalize(jdate); |
|
504 |
return jdate; |
|
505 |
} |
|
506 |
||
507 |
//----------------------------------------------------------------------- |
|
508 |
@Override |
|
509 |
public JapaneseDate with(TemporalField field, long newValue) { |
|
510 |
if (field instanceof ChronoField) { |
|
511 |
ChronoField f = (ChronoField) field; |
|
19030 | 512 |
if (getLong(f) == newValue) { // getLong() validates for supported fields |
15658 | 513 |
return this; |
514 |
} |
|
515 |
switch (f) { |
|
516 |
case YEAR_OF_ERA: |
|
517 |
case YEAR: |
|
518 |
case ERA: { |
|
16852 | 519 |
int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); |
15658 | 520 |
switch (f) { |
521 |
case YEAR_OF_ERA: |
|
522 |
return this.withYear(nvalue); |
|
523 |
case YEAR: |
|
524 |
return with(isoDate.withYear(nvalue)); |
|
525 |
case ERA: { |
|
526 |
return this.withYear(JapaneseEra.of(nvalue), yearOfEra); |
|
527 |
} |
|
528 |
} |
|
529 |
} |
|
530 |
} |
|
16852 | 531 |
// YEAR, PROLEPTIC_MONTH and others are same as ISO |
15658 | 532 |
return with(isoDate.with(field, newValue)); |
533 |
} |
|
19030 | 534 |
return super.with(field, newValue); |
15658 | 535 |
} |
536 |
||
537 |
/** |
|
538 |
* {@inheritDoc} |
|
539 |
* @throws DateTimeException {@inheritDoc} |
|
540 |
* @throws ArithmeticException {@inheritDoc} |
|
541 |
*/ |
|
542 |
@Override |
|
543 |
public JapaneseDate with(TemporalAdjuster adjuster) { |
|
16852 | 544 |
return super.with(adjuster); |
15658 | 545 |
} |
546 |
||
547 |
/** |
|
548 |
* {@inheritDoc} |
|
549 |
* @throws DateTimeException {@inheritDoc} |
|
550 |
* @throws ArithmeticException {@inheritDoc} |
|
551 |
*/ |
|
552 |
@Override |
|
553 |
public JapaneseDate plus(TemporalAmount amount) { |
|
16852 | 554 |
return super.plus(amount); |
15658 | 555 |
} |
556 |
||
557 |
/** |
|
558 |
* {@inheritDoc} |
|
559 |
* @throws DateTimeException {@inheritDoc} |
|
560 |
* @throws ArithmeticException {@inheritDoc} |
|
561 |
*/ |
|
562 |
@Override |
|
563 |
public JapaneseDate minus(TemporalAmount amount) { |
|
16852 | 564 |
return super.minus(amount); |
15658 | 565 |
} |
566 |
//----------------------------------------------------------------------- |
|
567 |
/** |
|
568 |
* Returns a copy of this date with the year altered. |
|
569 |
* <p> |
|
570 |
* This method changes the year of the date. |
|
571 |
* If the month-day is invalid for the year, then the previous valid day |
|
572 |
* will be selected instead. |
|
573 |
* <p> |
|
574 |
* This instance is immutable and unaffected by this method call. |
|
575 |
* |
|
576 |
* @param era the era to set in the result, not null |
|
577 |
* @param yearOfEra the year-of-era to set in the returned date |
|
578 |
* @return a {@code JapaneseDate} based on this date with the requested year, never null |
|
579 |
* @throws DateTimeException if {@code year} is invalid |
|
580 |
*/ |
|
581 |
private JapaneseDate withYear(JapaneseEra era, int yearOfEra) { |
|
582 |
int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra); |
|
583 |
return with(isoDate.withYear(year)); |
|
584 |
} |
|
585 |
||
586 |
/** |
|
587 |
* Returns a copy of this date with the year-of-era altered. |
|
588 |
* <p> |
|
589 |
* This method changes the year-of-era of the date. |
|
590 |
* If the month-day is invalid for the year, then the previous valid day |
|
591 |
* will be selected instead. |
|
592 |
* <p> |
|
593 |
* This instance is immutable and unaffected by this method call. |
|
594 |
* |
|
595 |
* @param year the year to set in the returned date |
|
596 |
* @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null |
|
597 |
* @throws DateTimeException if {@code year} is invalid |
|
598 |
*/ |
|
599 |
private JapaneseDate withYear(int year) { |
|
16852 | 600 |
return withYear(getEra(), year); |
15658 | 601 |
} |
602 |
||
603 |
//----------------------------------------------------------------------- |
|
604 |
@Override |
|
605 |
JapaneseDate plusYears(long years) { |
|
606 |
return with(isoDate.plusYears(years)); |
|
607 |
} |
|
608 |
||
609 |
@Override |
|
610 |
JapaneseDate plusMonths(long months) { |
|
611 |
return with(isoDate.plusMonths(months)); |
|
612 |
} |
|
613 |
||
614 |
@Override |
|
615 |
JapaneseDate plusWeeks(long weeksToAdd) { |
|
616 |
return with(isoDate.plusWeeks(weeksToAdd)); |
|
617 |
} |
|
618 |
||
619 |
@Override |
|
620 |
JapaneseDate plusDays(long days) { |
|
621 |
return with(isoDate.plusDays(days)); |
|
622 |
} |
|
623 |
||
624 |
@Override |
|
625 |
public JapaneseDate plus(long amountToAdd, TemporalUnit unit) { |
|
16852 | 626 |
return super.plus(amountToAdd, unit); |
15658 | 627 |
} |
628 |
||
629 |
@Override |
|
630 |
public JapaneseDate minus(long amountToAdd, TemporalUnit unit) { |
|
16852 | 631 |
return super.minus(amountToAdd, unit); |
15658 | 632 |
} |
633 |
||
634 |
@Override |
|
635 |
JapaneseDate minusYears(long yearsToSubtract) { |
|
16852 | 636 |
return super.minusYears(yearsToSubtract); |
15658 | 637 |
} |
638 |
||
639 |
@Override |
|
640 |
JapaneseDate minusMonths(long monthsToSubtract) { |
|
16852 | 641 |
return super.minusMonths(monthsToSubtract); |
15658 | 642 |
} |
643 |
||
644 |
@Override |
|
645 |
JapaneseDate minusWeeks(long weeksToSubtract) { |
|
16852 | 646 |
return super.minusWeeks(weeksToSubtract); |
15658 | 647 |
} |
648 |
||
649 |
@Override |
|
650 |
JapaneseDate minusDays(long daysToSubtract) { |
|
16852 | 651 |
return super.minusDays(daysToSubtract); |
15658 | 652 |
} |
653 |
||
654 |
private JapaneseDate with(LocalDate newDate) { |
|
655 |
return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate)); |
|
656 |
} |
|
657 |
||
658 |
@Override // for javadoc and covariant return type |
|
19030 | 659 |
@SuppressWarnings("unchecked") |
15658 | 660 |
public final ChronoLocalDateTime<JapaneseDate> atTime(LocalTime localTime) { |
19030 | 661 |
return (ChronoLocalDateTime<JapaneseDate>)super.atTime(localTime); |
15658 | 662 |
} |
663 |
||
664 |
@Override |
|
19030 | 665 |
public Period until(ChronoLocalDate endDate) { |
666 |
return isoDate.until(endDate); |
|
15658 | 667 |
} |
668 |
||
669 |
@Override // override for performance |
|
670 |
public long toEpochDay() { |
|
671 |
return isoDate.toEpochDay(); |
|
672 |
} |
|
673 |
||
674 |
//------------------------------------------------------------------------- |
|
675 |
@Override // override for performance |
|
676 |
public boolean equals(Object obj) { |
|
677 |
if (this == obj) { |
|
678 |
return true; |
|
679 |
} |
|
680 |
if (obj instanceof JapaneseDate) { |
|
681 |
JapaneseDate otherDate = (JapaneseDate) obj; |
|
682 |
return this.isoDate.equals(otherDate.isoDate); |
|
683 |
} |
|
684 |
return false; |
|
685 |
} |
|
686 |
||
687 |
@Override // override for performance |
|
688 |
public int hashCode() { |
|
689 |
return getChronology().getId().hashCode() ^ isoDate.hashCode(); |
|
690 |
} |
|
691 |
||
692 |
//----------------------------------------------------------------------- |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
693 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
694 |
* Defend against malicious streams. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
695 |
* @return never |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
696 |
* @throws InvalidObjectException always |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
697 |
*/ |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
698 |
private Object readResolve() throws InvalidObjectException { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
699 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
700 |
} |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
701 |
|
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
702 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
703 |
* Writes the object using a |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
704 |
* <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
705 |
* @serialData |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
706 |
* <pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
707 |
* out.writeByte(4); // identifies a JapaneseDate |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
708 |
* out.writeInt(get(YEAR)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
709 |
* out.writeByte(get(MONTH_OF_YEAR)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
710 |
* out.writeByte(get(DAY_OF_MONTH)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
711 |
* </pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
712 |
* |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
713 |
* @return the instance of {@code Ser}, not null |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
714 |
*/ |
15658 | 715 |
private Object writeReplace() { |
716 |
return new Ser(Ser.JAPANESE_DATE_TYPE, this); |
|
717 |
} |
|
718 |
||
719 |
void writeExternal(DataOutput out) throws IOException { |
|
720 |
// JapaneseChronology is implicit in the JAPANESE_DATE_TYPE |
|
721 |
out.writeInt(get(YEAR)); |
|
722 |
out.writeByte(get(MONTH_OF_YEAR)); |
|
723 |
out.writeByte(get(DAY_OF_MONTH)); |
|
724 |
} |
|
725 |
||
726 |
static JapaneseDate readExternal(DataInput in) throws IOException { |
|
727 |
int year = in.readInt(); |
|
728 |
int month = in.readByte(); |
|
729 |
int dayOfMonth = in.readByte(); |
|
730 |
return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth); |
|
731 |
} |
|
732 |
||
733 |
} |