|
1 | 1 | /* |
2 | 2 | * ----------------------------------------------------------------------- |
3 | | - * Copyright © 2013-2018 Meno Hochschild, <http://www.menodata.de/> |
| 3 | + * Copyright © 2013-2021 Meno Hochschild, <http://www.menodata.de/> |
4 | 4 | * ----------------------------------------------------------------------- |
5 | 5 | * This file (CalendarDate.java) is part of project Time4J. |
6 | 6 | * |
|
21 | 21 |
|
22 | 22 | package net.time4j.engine; |
23 | 23 |
|
| 24 | +import java.time.chrono.ChronoLocalDate; |
| 25 | + |
24 | 26 |
|
25 | 27 | /** |
26 | 28 | * <p>Represents a general calendar date. </p> |
@@ -88,12 +90,7 @@ default <T extends Calendrical<?, T>> T transform(Chronology<T> target) { |
88 | 90 |
|
89 | 91 | long utcDays = this.getDaysSinceEpochUTC(); |
90 | 92 | CalendarSystem<T> calsys = target.getCalendarSystem(); |
91 | | - |
92 | | - if ((calsys.getMinimumSinceUTC() > utcDays) || (calsys.getMaximumSinceUTC() < utcDays)) { |
93 | | - throw new ArithmeticException("Cannot transform <" + utcDays + "> to: " + target.getChronoType().getName()); |
94 | | - } else { |
95 | | - return calsys.transform(utcDays); |
96 | | - } |
| 93 | + return DateTools.convert(utcDays, calsys, target); |
97 | 94 |
|
98 | 95 | } |
99 | 96 |
|
@@ -137,12 +134,7 @@ default <T extends CalendarVariant<T>> T transform( |
137 | 134 |
|
138 | 135 | long utcDays = this.getDaysSinceEpochUTC(); |
139 | 136 | CalendarSystem<T> calsys = target.getCalendarSystem(variant); |
140 | | - |
141 | | - if ((calsys.getMinimumSinceUTC() > utcDays) || (calsys.getMaximumSinceUTC() < utcDays)) { |
142 | | - throw new ArithmeticException("Cannot transform <" + utcDays + "> to: " + target.getChronoType().getName()); |
143 | | - } else { |
144 | | - return calsys.transform(utcDays); |
145 | | - } |
| 137 | + return DateTools.convert(utcDays, calsys, target); |
146 | 138 |
|
147 | 139 | } |
148 | 140 |
|
@@ -232,12 +224,7 @@ default <T extends Calendrical<?, T>> T transform(Class<T> target) { |
232 | 224 |
|
233 | 225 | long utcDays = this.getDaysSinceEpochUTC(); |
234 | 226 | CalendarSystem<T> calsys = chronology.getCalendarSystem(); |
235 | | - |
236 | | - if ((calsys.getMinimumSinceUTC() > utcDays) || (calsys.getMaximumSinceUTC() < utcDays)) { |
237 | | - throw new ArithmeticException("Cannot transform <" + utcDays + "> to: " + ref); |
238 | | - } else { |
239 | | - return calsys.transform(utcDays); |
240 | | - } |
| 227 | + return DateTools.convert(utcDays, calsys, chronology); |
241 | 228 |
|
242 | 229 | } |
243 | 230 |
|
@@ -292,12 +279,7 @@ default <T extends CalendarVariant<T>> T transform( |
292 | 279 |
|
293 | 280 | long utcDays = this.getDaysSinceEpochUTC(); |
294 | 281 | CalendarSystem<T> calsys = chronology.getCalendarSystem(variant); |
295 | | - |
296 | | - if ((calsys.getMinimumSinceUTC() > utcDays) || (calsys.getMaximumSinceUTC() < utcDays)) { |
297 | | - throw new ArithmeticException("Cannot transform <" + utcDays + "> to: " + ref); |
298 | | - } else { |
299 | | - return calsys.transform(utcDays); |
300 | | - } |
| 282 | + return DateTools.convert(utcDays, calsys, chronology); |
301 | 283 |
|
302 | 284 | } |
303 | 285 |
|
@@ -345,4 +327,110 @@ default <T extends CalendarVariant<T>> T transform( |
345 | 327 |
|
346 | 328 | } |
347 | 329 |
|
| 330 | + /** |
| 331 | + * <p>Converts the calendar date corresponding to {@code ChronoLocalDate} |
| 332 | + * in given target chronology and variant. </p> |
| 333 | + * |
| 334 | + * @param <T> type of target chronology |
| 335 | + * @param threeten the {@code ChronoLocalDate} to be converted |
| 336 | + * @param target chronology given date shall be converted to |
| 337 | + * @param variantSource source of desired calendar variant |
| 338 | + * @return converted date of target type T |
| 339 | + * @throws ChronoException if the variant of given source is not recognized |
| 340 | + * @throws ArithmeticException in case of numerical overflow |
| 341 | + * @since 5.8 |
| 342 | + */ |
| 343 | + /*[deutsch] |
| 344 | + * <p>Konvertiert das gegebene Kalenderdatum entsprechend {@code ChronoLocalDate} |
| 345 | + * zur angegebenen Zielchronologie und Variante. </p> |
| 346 | + * |
| 347 | + * @param <T> type of target chronology |
| 348 | + * @param threeten the {@code ChronoLocalDate} to be converted |
| 349 | + * @param target chronology given date shall be converted to |
| 350 | + * @param variantSource source of desired calendar variant |
| 351 | + * @return converted date of target type T |
| 352 | + * @throws ChronoException if the variant of given source is not recognized |
| 353 | + * @throws ArithmeticException in case of numerical overflow |
| 354 | + * @since 5.8 |
| 355 | + */ |
| 356 | + static <T extends CalendarVariant<T>> T from( |
| 357 | + ChronoLocalDate threeten, |
| 358 | + CalendarFamily<T> target, |
| 359 | + VariantSource variantSource |
| 360 | + ) { |
| 361 | + |
| 362 | + return from(threeten, target, variantSource.getVariant()); |
| 363 | + |
| 364 | + } |
| 365 | + |
| 366 | + /** |
| 367 | + * <p>Converts the calendar date corresponding to {@code ChronoLocalDate} |
| 368 | + * in given target chronology and variant. </p> |
| 369 | + * |
| 370 | + * @param <T> type of target chronology |
| 371 | + * @param threeten the {@code ChronoLocalDate} to be converted |
| 372 | + * @param target chronology given date shall be converted to |
| 373 | + * @param variant desired calendar variant |
| 374 | + * @return converted date of target type T |
| 375 | + * @throws ChronoException if the variant of given source is not recognized |
| 376 | + * @throws ArithmeticException in case of numerical overflow |
| 377 | + * @since 5.8 |
| 378 | + */ |
| 379 | + /*[deutsch] |
| 380 | + * <p>Konvertiert das gegebene Kalenderdatum entsprechend {@code ChronoLocalDate} |
| 381 | + * zur angegebenen Zielchronologie und Variante. </p> |
| 382 | + * |
| 383 | + * @param <T> type of target chronology |
| 384 | + * @param threeten the {@code ChronoLocalDate} to be converted |
| 385 | + * @param target chronology given date shall be converted to |
| 386 | + * @param variant desired calendar variant |
| 387 | + * @return converted date of target type T |
| 388 | + * @throws ChronoException if the variant of given source is not recognized |
| 389 | + * @throws ArithmeticException in case of numerical overflow |
| 390 | + * @since 5.8 |
| 391 | + */ |
| 392 | + static <T extends CalendarVariant<T>> T from( |
| 393 | + ChronoLocalDate threeten, |
| 394 | + CalendarFamily<T> target, |
| 395 | + String variant |
| 396 | + ) { |
| 397 | + |
| 398 | + long utcDays = EpochDays.UTC.transform(threeten.toEpochDay(), EpochDays.UNIX); |
| 399 | + CalendarSystem<T> calsys = target.getCalendarSystem(variant); |
| 400 | + return DateTools.convert(utcDays, calsys, target); |
| 401 | + |
| 402 | + } |
| 403 | + |
| 404 | + /** |
| 405 | + * <p>Converts the calendar date corresponding to {@code ChronoLocalDate} in given target chronology. </p> |
| 406 | + * |
| 407 | + * @param <T> type of target chronology |
| 408 | + * @param threeten the {@code ChronoLocalDate} to be converted |
| 409 | + * @param target chronology given date shall be converted to |
| 410 | + * @return converted date of target type T |
| 411 | + * @throws ArithmeticException in case of numerical overflow |
| 412 | + * @since 5.8 |
| 413 | + */ |
| 414 | + /*[deutsch] |
| 415 | + * <p>Konvertiert das gegebene Kalenderdatum entsprechend {@code ChronoLocalDate} |
| 416 | + * zur angegebenen Zielchronologie. </p> |
| 417 | + * |
| 418 | + * @param <T> type of target chronology |
| 419 | + * @param threeten the {@code ChronoLocalDate} to be converted |
| 420 | + * @param target chronology given date shall be converted to |
| 421 | + * @return converted date of target type T |
| 422 | + * @throws ArithmeticException in case of numerical overflow |
| 423 | + * @since 5.8 |
| 424 | + */ |
| 425 | + static <T extends Calendrical<?, T>> T from( |
| 426 | + ChronoLocalDate threeten, |
| 427 | + Chronology<T> target |
| 428 | + ) { |
| 429 | + |
| 430 | + long utcDays = EpochDays.UTC.transform(threeten.toEpochDay(), EpochDays.UNIX); |
| 431 | + CalendarSystem<T> calsys = target.getCalendarSystem(); |
| 432 | + return DateTools.convert(utcDays, calsys, target); |
| 433 | + |
| 434 | + } |
| 435 | + |
348 | 436 | } |
0 commit comments