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 /**
41 * {@code JapaneseImperialCalendar} implements a Japanese
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.
45 * <pre>{@code
46 * ERA value Era name Since (in Gregorian)
47 * ------------------------------------------------------
48 * 0 N/A N/A
49 * 1 Meiji 1868-01-01T00:00:00 local time
50 * 2 Taisho 1912-07-30T00:00:00 local time
51 * 3 Showa 1926-12-25T00:00:00 local time
52 * 4 Heisei 1989-01-08T00:00:00 local time
53 * 5 NewEra 2019-05-01T00:00:00 local time
54 * ------------------------------------------------------
55 * }</pre>
56 *
57 * <p>{@code ERA} value 0 specifies the years before Meiji and
58 * the Gregorian year values are used. Unlike
59 * {@link GregorianCalendar}, the Julian to Gregorian transition is not
60 * supported because it doesn't make any sense to the Japanese
61 * calendar systems used before Meiji. To represent the years before
62 * Gregorian year 1, 0 and negative values are used. The Japanese
63 * Imperial rescripts and government decrees don't specify how to deal
64 * with time differences for applying the era transitions. This
65 * calendar implementation assumes local time for all transitions.
66 *
67 * <p>A new era can be specified using property
68 * jdk.calendar.japanese.supplemental.era. The new era is added to the
69 * predefined eras. The syntax of the property is as follows.
70 * <pre>
71 * {@code name=<name>,abbr=<abbr>,since=<time['u']>}
72 * </pre>
73 * where
112 * The ERA constant designating the Meiji era.
113 */
114 public static final int MEIJI = 1;
115
116 /**
117 * The ERA constant designating the Taisho era.
118 */
119 public static final int TAISHO = 2;
120
121 /**
122 * The ERA constant designating the Showa era.
123 */
124 public static final int SHOWA = 3;
125
126 /**
127 * The ERA constant designating the Heisei era.
128 */
129 public static final int HEISEI = 4;
130
131 /**
132 * The ERA constant designating the NewEra era.
133 */
134 private static final int NEWERA = 5;
135
136 private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian)
137
138 // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit
139 // into ints, they must be longs in order to prevent arithmetic overflow
140 // when performing (bug 4173516).
141 private static final int ONE_SECOND = 1000;
142 private static final int ONE_MINUTE = 60*ONE_SECOND;
143 private static final int ONE_HOUR = 60*ONE_MINUTE;
144 private static final long ONE_DAY = 24*ONE_HOUR;
145
146 // Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton).
147 private static final LocalGregorianCalendar jcal
148 = (LocalGregorianCalendar) CalendarSystem.forName("japanese");
149
150 // Gregorian calendar instance. This is required because era
151 // transition dates are given in Gregorian dates.
152 private static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
153
154 // The Era instance representing "before Meiji".
1744 // The spec is to calculate WEEK_OF_YEAR in the
1745 // ISO8601-style. This creates problems, though.
1746 if (weekOfYear == 0) {
1747 // If the date belongs to the last week of the
1748 // previous year, use the week number of "12/31" of
1749 // the "previous" year. Again, if the previous year is
1750 // a transition year, we need to take care of it.
1751 // Usually the previous day of the first day of a year
1752 // is December 31, which is not always true in the
1753 // Japanese imperial calendar system.
1754 long fixedDec31 = fixedDateJan1 - 1;
1755 long prevJan1;
1756 LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31);
1757 if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) {
1758 prevJan1 = fixedDateJan1 - 365;
1759 if (d.isLeapYear()) {
1760 --prevJan1;
1761 }
1762 } else if (transitionYear) {
1763 if (jdate.getYear() == 1) {
1764 // As of NewEra (since Meiji) there's no case
1765 // that there are multiple transitions in a
1766 // year. Historically there was such
1767 // case. There might be such case again in the
1768 // future.
1769 if (era > NEWERA) {
1770 CalendarDate pd = eras[era - 1].getSinceDate();
1771 if (normalizedYear == pd.getYear()) {
1772 d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth());
1773 }
1774 } else {
1775 d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1);
1776 }
1777 jcal.normalize(d);
1778 prevJan1 = jcal.getFixedDate(d);
1779 } else {
1780 prevJan1 = fixedDateJan1 - 365;
1781 if (d.isLeapYear()) {
1782 --prevJan1;
1783 }
1784 }
1785 } else {
1786 CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate();
1787 d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth());
1788 jcal.normalize(d);
1789 prevJan1 = jcal.getFixedDate(d);
|
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 /**
41 * {@code JapaneseImperialCalendar} implements a Japanese
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.
45 * <pre>{@code
46 * ERA value Era name Since (in Gregorian)
47 * ------------------------------------------------------
48 * 0 N/A N/A
49 * 1 Meiji 1868-01-01T00:00:00 local time
50 * 2 Taisho 1912-07-30T00:00:00 local time
51 * 3 Showa 1926-12-25T00:00:00 local time
52 * 4 Heisei 1989-01-08T00:00:00 local time
53 * 5 Reiwa 2019-05-01T00:00:00 local time
54 * ------------------------------------------------------
55 * }</pre>
56 *
57 * <p>{@code ERA} value 0 specifies the years before Meiji and
58 * the Gregorian year values are used. Unlike
59 * {@link GregorianCalendar}, the Julian to Gregorian transition is not
60 * supported because it doesn't make any sense to the Japanese
61 * calendar systems used before Meiji. To represent the years before
62 * Gregorian year 1, 0 and negative values are used. The Japanese
63 * Imperial rescripts and government decrees don't specify how to deal
64 * with time differences for applying the era transitions. This
65 * calendar implementation assumes local time for all transitions.
66 *
67 * <p>A new era can be specified using property
68 * jdk.calendar.japanese.supplemental.era. The new era is added to the
69 * predefined eras. The syntax of the property is as follows.
70 * <pre>
71 * {@code name=<name>,abbr=<abbr>,since=<time['u']>}
72 * </pre>
73 * where
112 * The ERA constant designating the Meiji era.
113 */
114 public static final int MEIJI = 1;
115
116 /**
117 * The ERA constant designating the Taisho era.
118 */
119 public static final int TAISHO = 2;
120
121 /**
122 * The ERA constant designating the Showa era.
123 */
124 public static final int SHOWA = 3;
125
126 /**
127 * The ERA constant designating the Heisei era.
128 */
129 public static final int HEISEI = 4;
130
131 /**
132 * The ERA constant designating the Reiwa era.
133 */
134 private static final int REIWA = 5;
135
136 private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian)
137
138 // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit
139 // into ints, they must be longs in order to prevent arithmetic overflow
140 // when performing (bug 4173516).
141 private static final int ONE_SECOND = 1000;
142 private static final int ONE_MINUTE = 60*ONE_SECOND;
143 private static final int ONE_HOUR = 60*ONE_MINUTE;
144 private static final long ONE_DAY = 24*ONE_HOUR;
145
146 // Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton).
147 private static final LocalGregorianCalendar jcal
148 = (LocalGregorianCalendar) CalendarSystem.forName("japanese");
149
150 // Gregorian calendar instance. This is required because era
151 // transition dates are given in Gregorian dates.
152 private static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
153
154 // The Era instance representing "before Meiji".
1744 // The spec is to calculate WEEK_OF_YEAR in the
1745 // ISO8601-style. This creates problems, though.
1746 if (weekOfYear == 0) {
1747 // If the date belongs to the last week of the
1748 // previous year, use the week number of "12/31" of
1749 // the "previous" year. Again, if the previous year is
1750 // a transition year, we need to take care of it.
1751 // Usually the previous day of the first day of a year
1752 // is December 31, which is not always true in the
1753 // Japanese imperial calendar system.
1754 long fixedDec31 = fixedDateJan1 - 1;
1755 long prevJan1;
1756 LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31);
1757 if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) {
1758 prevJan1 = fixedDateJan1 - 365;
1759 if (d.isLeapYear()) {
1760 --prevJan1;
1761 }
1762 } else if (transitionYear) {
1763 if (jdate.getYear() == 1) {
1764 // As of Reiwa (since Meiji) there's no case
1765 // that there are multiple transitions in a
1766 // year. Historically there was such
1767 // case. There might be such case again in the
1768 // future.
1769 if (era > REIWA) {
1770 CalendarDate pd = eras[era - 1].getSinceDate();
1771 if (normalizedYear == pd.getYear()) {
1772 d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth());
1773 }
1774 } else {
1775 d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1);
1776 }
1777 jcal.normalize(d);
1778 prevJan1 = jcal.getFixedDate(d);
1779 } else {
1780 prevJan1 = fixedDateJan1 - 365;
1781 if (d.isLeapYear()) {
1782 --prevJan1;
1783 }
1784 }
1785 } else {
1786 CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate();
1787 d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth());
1788 jcal.normalize(d);
1789 prevJan1 = jcal.getFixedDate(d);
|