81 import java.time.temporal.UnsupportedTemporalTypeException; |
81 import java.time.temporal.UnsupportedTemporalTypeException; |
82 import java.time.temporal.ValueRange; |
82 import java.time.temporal.ValueRange; |
83 import java.util.Calendar; |
83 import java.util.Calendar; |
84 import java.util.Objects; |
84 import java.util.Objects; |
85 |
85 |
|
86 import sun.util.calendar.CalendarDate; |
86 import sun.util.calendar.LocalGregorianCalendar; |
87 import sun.util.calendar.LocalGregorianCalendar; |
87 |
88 |
88 /** |
89 /** |
89 * A date in the Japanese Imperial calendar system. |
90 * A date in the Japanese Imperial calendar system. |
90 * <p> |
91 * <p> |
99 * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br> |
100 * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br> |
100 * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br> |
101 * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br> |
101 * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to |
102 * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to |
102 * {@code JapaneseChronology.ERA_HEISEI}.<br> |
103 * {@code JapaneseChronology.ERA_HEISEI}.<br> |
103 * |
104 * |
104 * <h3>Specification for implementors</h3> |
105 * @implSpec |
105 * This class is immutable and thread-safe. |
106 * This class is immutable and thread-safe. |
106 * |
107 * |
107 * @since 1.8 |
108 * @since 1.8 |
108 */ |
109 */ |
109 public final class JapaneseDate |
110 public final class JapaneseDate |
230 * or if the day-of-year is invalid for the year |
231 * or if the day-of-year is invalid for the year |
231 */ |
232 */ |
232 public static JapaneseDate ofYearDay(int prolepticYear, int dayOfYear) { |
233 public static JapaneseDate ofYearDay(int prolepticYear, int dayOfYear) { |
233 LocalDate date = LocalDate.ofYearDay(prolepticYear, dayOfYear); |
234 LocalDate date = LocalDate.ofYearDay(prolepticYear, dayOfYear); |
234 return of(prolepticYear, date.getMonthValue(), date.getDayOfMonth()); |
235 return of(prolepticYear, date.getMonthValue(), date.getDayOfMonth()); |
|
236 } |
|
237 |
|
238 static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) { |
|
239 CalendarDate firstDay = era.getPrivateEra().getSinceDate(); |
|
240 LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null); |
|
241 jdate.setEra(era.getPrivateEra()); |
|
242 if (yearOfEra == 1) { |
|
243 jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1); |
|
244 } else { |
|
245 jdate.setDate(yearOfEra, 1, dayOfYear); |
|
246 } |
|
247 JapaneseChronology.JCAL.normalize(jdate); |
|
248 if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) { |
|
249 throw new DateTimeException("Invalid parameters"); |
|
250 } |
|
251 LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(), |
|
252 jdate.getMonth(), jdate.getDayOfMonth()); |
|
253 return new JapaneseDate(era, yearOfEra, localdate); |
235 } |
254 } |
236 |
255 |
237 /** |
256 /** |
238 * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar |
257 * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar |
239 * system from the era, year-of-era, month-of-year and day-of-month fields. |
258 * system from the era, year-of-era, month-of-year and day-of-month fields. |