1 /* |
1 /* |
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
36 import sun.util.calendar.Gregorian; |
36 import sun.util.calendar.Gregorian; |
37 import sun.util.calendar.LocalGregorianCalendar; |
37 import sun.util.calendar.LocalGregorianCalendar; |
38 import sun.util.calendar.ZoneInfo; |
38 import sun.util.calendar.ZoneInfo; |
39 |
39 |
40 /** |
40 /** |
41 * <code>JapaneseImperialCalendar</code> implements a Japanese |
41 * {@code JapaneseImperialCalendar} implements a Japanese |
42 * calendar system in which the imperial era-based year numbering is |
42 * calendar system in which the imperial era-based year numbering is |
43 * supported from the Meiji era. The following are the eras supported |
43 * supported from the Meiji era. The following are the eras supported |
44 * by this calendar system. |
44 * by this calendar system. |
45 * <pre><tt> |
45 * <pre>{@code |
46 * ERA value Era name Since (in Gregorian) |
46 * ERA value Era name Since (in Gregorian) |
47 * ------------------------------------------------------ |
47 * ------------------------------------------------------ |
48 * 0 N/A N/A |
48 * 0 N/A N/A |
49 * 1 Meiji 1868-01-01 midnight local time |
49 * 1 Meiji 1868-01-01T00:00:00 local time |
50 * 2 Taisho 1912-07-30 midnight local time |
50 * 2 Taisho 1912-07-30T00:00:00 local time |
51 * 3 Showa 1926-12-25 midnight local time |
51 * 3 Showa 1926-12-25T00:00:00 local time |
52 * 4 Heisei 1989-01-08 midnight local time |
52 * 4 Heisei 1989-01-08T00:00:00 local time |
53 * ------------------------------------------------------ |
53 * ------------------------------------------------------ |
54 * </tt></pre> |
54 * }</pre> |
55 * |
55 * |
56 * <p><code>ERA</code> value 0 specifies the years before Meiji and |
56 * <p><code>ERA</code> value 0 specifies the years before Meiji and |
57 * the Gregorian year values are used. Unlike {@link |
57 * the Gregorian year values are used. Unlike {@link |
58 * GregorianCalendar}, the Julian to Gregorian transition is not |
58 * GregorianCalendar}, the Julian to Gregorian transition is not |
59 * supported because it doesn't make any sense to the Japanese |
59 * supported because it doesn't make any sense to the Japanese |
61 * Gregorian year 1, 0 and negative values are used. The Japanese |
61 * Gregorian year 1, 0 and negative values are used. The Japanese |
62 * Imperial rescripts and government decrees don't specify how to deal |
62 * Imperial rescripts and government decrees don't specify how to deal |
63 * with time differences for applying the era transitions. This |
63 * with time differences for applying the era transitions. This |
64 * calendar implementation assumes local time for all transitions. |
64 * calendar implementation assumes local time for all transitions. |
65 * |
65 * |
|
66 * <p>A new era can be specified using property |
|
67 * jdk.calendar.japanese.supplemental.era. The new era is added to the |
|
68 * predefined eras. The syntax of the property is as follows. |
|
69 * <p><pre> |
|
70 * {@code name=<name>,abbr=<abbr>,since=<time['u']>} |
|
71 * </pre> |
|
72 * where |
|
73 * <dl> |
|
74 * <dt>{@code <name>:}<dd>the full name of the new era (non-ASCII characters allowed) |
|
75 * <dt>{@code <abbr>:}<dd>the abbreviation of the new era (non-ASCII characters allowed) |
|
76 * <dt>{@code <time['u']>:}<dd>the start time of the new era represented by |
|
77 * milliseconds from 1970-01-01T00:00:00 local time or UTC if {@code 'u'} is |
|
78 * appended to the milliseconds value. (ASCII digits only) |
|
79 * </dl> |
|
80 * |
|
81 * <p>If the given era is invalid, such as the since value before the |
|
82 * beginning of the last predefined era, the given era will be |
|
83 * ignored. |
|
84 * |
|
85 * <p>The following is an example of the property usage. |
|
86 * <p><pre> |
|
87 * java -Djdk.calendar.japanese.supplemental.era="name=NewEra,abbr=N,since=253374307200000" |
|
88 * </pre> |
|
89 * The property specifies an era change to NewEra at 9999-02-11T00:00:00 local time. |
|
90 * |
66 * @author Masayoshi Okutsu |
91 * @author Masayoshi Okutsu |
67 * @since 1.6 |
92 * @since 1.6 |
68 */ |
93 */ |
69 class JapaneseImperialCalendar extends Calendar { |
94 class JapaneseImperialCalendar extends Calendar { |
70 /* |
95 /* |
100 * The ERA constant designating the Heisei era. |
125 * The ERA constant designating the Heisei era. |
101 */ |
126 */ |
102 public static final int HEISEI = 4; |
127 public static final int HEISEI = 4; |
103 |
128 |
104 private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian) |
129 private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian) |
105 private static final int EPOCH_YEAR = 1970; |
|
106 |
130 |
107 // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit |
131 // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit |
108 // into ints, they must be longs in order to prevent arithmetic overflow |
132 // into ints, they must be longs in order to prevent arithmetic overflow |
109 // when performing (bug 4173516). |
133 // when performing (bug 4173516). |
110 private static final int ONE_SECOND = 1000; |
134 private static final int ONE_SECOND = 1000; |
111 private static final int ONE_MINUTE = 60*ONE_SECOND; |
135 private static final int ONE_MINUTE = 60*ONE_SECOND; |
112 private static final int ONE_HOUR = 60*ONE_MINUTE; |
136 private static final int ONE_HOUR = 60*ONE_MINUTE; |
113 private static final long ONE_DAY = 24*ONE_HOUR; |
137 private static final long ONE_DAY = 24*ONE_HOUR; |
114 private static final long ONE_WEEK = 7*ONE_DAY; |
|
115 |
138 |
116 // Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton). |
139 // Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton). |
117 private static final LocalGregorianCalendar jcal |
140 private static final LocalGregorianCalendar jcal |
118 = (LocalGregorianCalendar) CalendarSystem.forName("japanese"); |
141 = (LocalGregorianCalendar) CalendarSystem.forName("japanese"); |
119 |
142 |
215 14*ONE_HOUR, // ZONE_OFFSET |
238 14*ONE_HOUR, // ZONE_OFFSET |
216 2*ONE_HOUR // DST_OFFSET (double summer time) |
239 2*ONE_HOUR // DST_OFFSET (double summer time) |
217 }; |
240 }; |
218 |
241 |
219 // Proclaim serialization compatibility with JDK 1.6 |
242 // Proclaim serialization compatibility with JDK 1.6 |
|
243 @SuppressWarnings("FieldNameHidesFieldInSuperclass") |
220 private static final long serialVersionUID = -3364572813905467929L; |
244 private static final long serialVersionUID = -3364572813905467929L; |
221 |
245 |
222 static { |
246 static { |
223 Era[] es = jcal.getEras(); |
247 Era[] es = jcal.getEras(); |
224 int length = es.length + 1; |
248 int length = es.length + 1; |
338 * @param obj the object to compare with. |
362 * @param obj the object to compare with. |
339 * @return <code>true</code> if this object is equal to <code>obj</code>; |
363 * @return <code>true</code> if this object is equal to <code>obj</code>; |
340 * <code>false</code> otherwise. |
364 * <code>false</code> otherwise. |
341 * @see Calendar#compareTo(Calendar) |
365 * @see Calendar#compareTo(Calendar) |
342 */ |
366 */ |
|
367 @Override |
343 public boolean equals(Object obj) { |
368 public boolean equals(Object obj) { |
344 return obj instanceof JapaneseImperialCalendar && |
369 return obj instanceof JapaneseImperialCalendar && |
345 super.equals(obj); |
370 super.equals(obj); |
346 } |
371 } |
347 |
372 |
348 /** |
373 /** |
349 * Generates the hash code for this |
374 * Generates the hash code for this |
350 * <code>JapaneseImperialCalendar</code> object. |
375 * <code>JapaneseImperialCalendar</code> object. |
351 */ |
376 */ |
|
377 @Override |
352 public int hashCode() { |
378 public int hashCode() { |
353 return super.hashCode() ^ jdate.hashCode(); |
379 return super.hashCode() ^ jdate.hashCode(); |
354 } |
380 } |
355 |
381 |
356 /** |
382 /** |
379 * @exception IllegalArgumentException if <code>field</code> is |
405 * @exception IllegalArgumentException if <code>field</code> is |
380 * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown, |
406 * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown, |
381 * or if any calendar fields have out-of-range values in |
407 * or if any calendar fields have out-of-range values in |
382 * non-lenient mode. |
408 * non-lenient mode. |
383 */ |
409 */ |
|
410 @Override |
384 public void add(int field, int amount) { |
411 public void add(int field, int amount) { |
385 // If amount == 0, do nothing even the given field is out of |
412 // If amount == 0, do nothing even the given field is out of |
386 // range. This is tested by JCK. |
413 // range. This is tested by JCK. |
387 if (amount == 0) { |
414 if (amount == 0) { |
388 return; // Do nothing! |
415 return; // Do nothing! |
531 * non-lenient mode. |
559 * non-lenient mode. |
532 * @see #roll(int,boolean) |
560 * @see #roll(int,boolean) |
533 * @see #add(int,int) |
561 * @see #add(int,int) |
534 * @see #set(int,int) |
562 * @see #set(int,int) |
535 */ |
563 */ |
|
564 @Override |
536 public void roll(int field, int amount) { |
565 public void roll(int field, int amount) { |
537 // If amount == 0, do nothing even the given field is out of |
566 // If amount == 0, do nothing even the given field is out of |
538 // range. This is tested by JCK. |
567 // range. This is tested by JCK. |
539 if (amount == 0) { |
568 if (amount == 0) { |
540 return; |
569 return; |