386 * This represents the concept of the day within the month.
387 * In the default ISO calendar system, this has values from 1 to 31 in most months.
388 * April, June, September, November have days from 1 to 30, while February has days
389 * from 1 to 28, or 29 in a leap year.
390 * <p>
391 * Non-ISO calendar systems should implement this field using the most recognized
392 * day-of-month values for users of the calendar system.
393 * Normally, this is a count of days from 1 to the length of the month.
394 */
395 DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),
396 /**
397 * The day-of-year.
398 * <p>
399 * This represents the concept of the day within the year.
400 * In the default ISO calendar system, this has values from 1 to 365 in standard
401 * years and 1 to 366 in leap years.
402 * <p>
403 * Non-ISO calendar systems should implement this field using the most recognized
404 * day-of-year values for users of the calendar system.
405 * Normally, this is a count of days from 1 to the length of the year.
406 */
407 DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)),
408 /**
409 * The epoch-day, based on the Java epoch of 1970-01-01 (ISO).
410 * <p>
411 * This field is the sequential count of days where 1970-01-01 (ISO) is zero.
412 * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
413 * <p>
414 * This field is strictly defined to have the same meaning in all calendar systems.
415 * This is necessary to ensure interoperation between calendars.
416 */
417 EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of((long) (Year.MIN_VALUE * 365.25), (long) (Year.MAX_VALUE * 365.25))),
418 /**
419 * The aligned week within a month.
420 * <p>
421 * This represents concept of the count of weeks within the period of a month
422 * where the weeks are aligned to the start of the month.
423 * This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_MONTH}.
424 * <p>
425 * For example, in a calendar systems with a seven day week, the first aligned-week-of-month
542 * <p>
543 * This represents the concept of the era, which is the largest division of the time-line.
544 * This field is typically used with {@link #YEAR_OF_ERA}.
545 * <p>
546 * In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
547 * The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
548 * The era 'BCE' is the previous era, and the year-of-era runs backwards.
549 * See {@link #YEAR_OF_ERA} for a full example.
550 * <p>
551 * Non-ISO calendar systems should implement this field to define eras.
552 * The value of the era that was active on 1970-01-01 (ISO) must be assigned the value 1.
553 * Earlier eras must have sequentially smaller values.
554 * Later eras must have sequentially larger values,
555 */
556 ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),
557 /**
558 * The instant epoch-seconds.
559 * <p>
560 * This represents the concept of the sequential count of seconds where
561 * 1970-01-01T00:00Z (ISO) is zero.
562 * This field may be used with {@link #NANO_OF_DAY} to represent the fraction of the day.
563 * <p>
564 * An {@link Instant} represents an instantaneous point on the time-line.
565 * On their own they have no elements which allow a local date-time to be obtained.
566 * Only when paired with an offset or time-zone can the local date or time be found.
567 * This field allows the seconds part of the instant to be queried.
568 * <p>
569 * This field is strictly defined to have the same meaning in all calendar systems.
570 * This is necessary to ensure interoperation between calendars.
571 */
572 INSTANT_SECONDS("InstantSeconds", SECONDS, FOREVER, ValueRange.of(Long.MIN_VALUE, Long.MAX_VALUE)),
573 /**
574 * The offset from UTC/Greenwich.
575 * <p>
576 * This represents the concept of the offset in seconds of local time from UTC/Greenwich.
577 * <p>
578 * A {@link ZoneOffset} represents the period of time that local time differs from UTC/Greenwich.
579 * This is usually a fixed number of hours and minutes.
580 * It is equivalent to the {@link ZoneOffset#getTotalSeconds() total amount} of the offset in seconds.
581 * For example, during the winter Paris has an offset of {@code +01:00}, which is 3600 seconds.
582 * <p>
583 * This field is strictly defined to have the same meaning in all calendar systems.
584 * This is necessary to ensure interoperation between calendars.
585 */
586 OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
587
591 private final ValueRange range;
592 private final String displayNameKey;
593
594 private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
595 this.name = name;
596 this.baseUnit = baseUnit;
597 this.rangeUnit = rangeUnit;
598 this.range = range;
599 this.displayNameKey = null;
600 }
601
602 private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
603 ValueRange range, String displayNameKey) {
604 this.name = name;
605 this.baseUnit = baseUnit;
606 this.rangeUnit = rangeUnit;
607 this.range = range;
608 this.displayNameKey = displayNameKey;
609 }
610
611 //-----------------------------------------------------------------------
612 @Override
613 public String getName() {
614 return name;
615 }
616
617 @Override
618 public String getDisplayName(Locale locale) {
619 Objects.requireNonNull(locale, "locale");
620 if (displayNameKey == null) {
621 return getName();
622 }
623
624 LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
625 .getLocaleResources(locale);
626 ResourceBundle rb = lr.getJavaTimeFormatData();
627 String key = "field." + displayNameKey;
628 return rb.containsKey(key) ? rb.getString(key) : getName();
629 }
630
631 @Override
632 public TemporalUnit getBaseUnit() {
633 return baseUnit;
634 }
635
636 @Override
637 public TemporalUnit getRangeUnit() {
638 return rangeUnit;
639 }
640
641 /**
642 * Gets the range of valid values for the field.
643 * <p>
644 * All fields can be expressed as a {@code long} integer.
645 * This method returns an object that describes the valid range for that value.
646 * <p>
647 * This method returns the range of the field in the ISO-8601 calendar system.
648 * This range may be incorrect for other calendar systems.
731
732 @Override
733 public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
734 return temporal.range(this);
735 }
736
737 @Override
738 public long getFrom(TemporalAccessor temporal) {
739 return temporal.getLong(this);
740 }
741
742 @SuppressWarnings("unchecked")
743 @Override
744 public <R extends Temporal> R adjustInto(R temporal, long newValue) {
745 return (R) temporal.with(this, newValue);
746 }
747
748 //-----------------------------------------------------------------------
749 @Override
750 public String toString() {
751 return getName();
752 }
753
754 }
|
386 * This represents the concept of the day within the month.
387 * In the default ISO calendar system, this has values from 1 to 31 in most months.
388 * April, June, September, November have days from 1 to 30, while February has days
389 * from 1 to 28, or 29 in a leap year.
390 * <p>
391 * Non-ISO calendar systems should implement this field using the most recognized
392 * day-of-month values for users of the calendar system.
393 * Normally, this is a count of days from 1 to the length of the month.
394 */
395 DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),
396 /**
397 * The day-of-year.
398 * <p>
399 * This represents the concept of the day within the year.
400 * In the default ISO calendar system, this has values from 1 to 365 in standard
401 * years and 1 to 366 in leap years.
402 * <p>
403 * Non-ISO calendar systems should implement this field using the most recognized
404 * day-of-year values for users of the calendar system.
405 * Normally, this is a count of days from 1 to the length of the year.
406 * <p>
407 * Note that a non-ISO calendar system may have year numbering system that changes
408 * at a different point to the natural reset in the month numbering. An example
409 * of this is the Japanese calendar system where a change of era, which resets
410 * the year number to 1, can happen on any date. The era and year reset also cause
411 * the day-of-year to be reset to 1, but not the month-of-year or day-of-month.
412 */
413 DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)),
414 /**
415 * The epoch-day, based on the Java epoch of 1970-01-01 (ISO).
416 * <p>
417 * This field is the sequential count of days where 1970-01-01 (ISO) is zero.
418 * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
419 * <p>
420 * This field is strictly defined to have the same meaning in all calendar systems.
421 * This is necessary to ensure interoperation between calendars.
422 */
423 EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of((long) (Year.MIN_VALUE * 365.25), (long) (Year.MAX_VALUE * 365.25))),
424 /**
425 * The aligned week within a month.
426 * <p>
427 * This represents concept of the count of weeks within the period of a month
428 * where the weeks are aligned to the start of the month.
429 * This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_MONTH}.
430 * <p>
431 * For example, in a calendar systems with a seven day week, the first aligned-week-of-month
548 * <p>
549 * This represents the concept of the era, which is the largest division of the time-line.
550 * This field is typically used with {@link #YEAR_OF_ERA}.
551 * <p>
552 * In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
553 * The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
554 * The era 'BCE' is the previous era, and the year-of-era runs backwards.
555 * See {@link #YEAR_OF_ERA} for a full example.
556 * <p>
557 * Non-ISO calendar systems should implement this field to define eras.
558 * The value of the era that was active on 1970-01-01 (ISO) must be assigned the value 1.
559 * Earlier eras must have sequentially smaller values.
560 * Later eras must have sequentially larger values,
561 */
562 ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),
563 /**
564 * The instant epoch-seconds.
565 * <p>
566 * This represents the concept of the sequential count of seconds where
567 * 1970-01-01T00:00Z (ISO) is zero.
568 * This field may be used with {@link #NANO_OF_SECOND} to represent the fraction of the second.
569 * <p>
570 * An {@link Instant} represents an instantaneous point on the time-line.
571 * On their own, an instant has insufficient information to allow a local date-time to be obtained.
572 * Only when paired with an offset or time-zone can the local date or time be calculated.
573 * <p>
574 * This field is strictly defined to have the same meaning in all calendar systems.
575 * This is necessary to ensure interoperation between calendars.
576 */
577 INSTANT_SECONDS("InstantSeconds", SECONDS, FOREVER, ValueRange.of(Long.MIN_VALUE, Long.MAX_VALUE)),
578 /**
579 * The offset from UTC/Greenwich.
580 * <p>
581 * This represents the concept of the offset in seconds of local time from UTC/Greenwich.
582 * <p>
583 * A {@link ZoneOffset} represents the period of time that local time differs from UTC/Greenwich.
584 * This is usually a fixed number of hours and minutes.
585 * It is equivalent to the {@link ZoneOffset#getTotalSeconds() total amount} of the offset in seconds.
586 * For example, during the winter Paris has an offset of {@code +01:00}, which is 3600 seconds.
587 * <p>
588 * This field is strictly defined to have the same meaning in all calendar systems.
589 * This is necessary to ensure interoperation between calendars.
590 */
591 OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
592
596 private final ValueRange range;
597 private final String displayNameKey;
598
599 private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
600 this.name = name;
601 this.baseUnit = baseUnit;
602 this.rangeUnit = rangeUnit;
603 this.range = range;
604 this.displayNameKey = null;
605 }
606
607 private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
608 ValueRange range, String displayNameKey) {
609 this.name = name;
610 this.baseUnit = baseUnit;
611 this.rangeUnit = rangeUnit;
612 this.range = range;
613 this.displayNameKey = displayNameKey;
614 }
615
616 @Override
617 public String getDisplayName(Locale locale) {
618 Objects.requireNonNull(locale, "locale");
619 if (displayNameKey == null) {
620 return name;
621 }
622
623 LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
624 .getLocaleResources(locale);
625 ResourceBundle rb = lr.getJavaTimeFormatData();
626 String key = "field." + displayNameKey;
627 return rb.containsKey(key) ? rb.getString(key) : name;
628 }
629
630 @Override
631 public TemporalUnit getBaseUnit() {
632 return baseUnit;
633 }
634
635 @Override
636 public TemporalUnit getRangeUnit() {
637 return rangeUnit;
638 }
639
640 /**
641 * Gets the range of valid values for the field.
642 * <p>
643 * All fields can be expressed as a {@code long} integer.
644 * This method returns an object that describes the valid range for that value.
645 * <p>
646 * This method returns the range of the field in the ISO-8601 calendar system.
647 * This range may be incorrect for other calendar systems.
730
731 @Override
732 public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
733 return temporal.range(this);
734 }
735
736 @Override
737 public long getFrom(TemporalAccessor temporal) {
738 return temporal.getLong(this);
739 }
740
741 @SuppressWarnings("unchecked")
742 @Override
743 public <R extends Temporal> R adjustInto(R temporal, long newValue) {
744 return (R) temporal.with(this, newValue);
745 }
746
747 //-----------------------------------------------------------------------
748 @Override
749 public String toString() {
750 return name;
751 }
752
753 }
|