src/share/classes/java/time/temporal/ChronoField.java

Print this page




  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.temporal;
  58 
  59 import static java.time.temporal.ChronoUnit.DAYS;
  60 import static java.time.temporal.ChronoUnit.ERAS;
  61 import static java.time.temporal.ChronoUnit.FOREVER;
  62 import static java.time.temporal.ChronoUnit.HALF_DAYS;
  63 import static java.time.temporal.ChronoUnit.HOURS;
  64 import static java.time.temporal.ChronoUnit.MICROS;
  65 import static java.time.temporal.ChronoUnit.MILLIS;
  66 import static java.time.temporal.ChronoUnit.MINUTES;
  67 import static java.time.temporal.ChronoUnit.MONTHS;
  68 import static java.time.temporal.ChronoUnit.NANOS;
  69 import static java.time.temporal.ChronoUnit.SECONDS;
  70 import static java.time.temporal.ChronoUnit.WEEKS;
  71 import static java.time.temporal.ChronoUnit.YEARS;
  72 
  73 import java.time.DayOfWeek;
  74 import java.time.Instant;

  75 import java.time.ZoneOffset;
  76 import java.time.format.DateTimeBuilder;

  77 
  78 /**
  79  * A standard set of fields.
  80  * <p>
  81  * This set of fields provide field-based access to manipulate a date, time or date-time.
  82  * The standard set of fields can be extended by implementing {@link TemporalField}.
  83  * <p>
  84  * These fields are intended to be applicable in multiple calendar systems.
  85  * For example, most non-ISO calendar systems define dates as a year, month and day,
  86  * just with slightly different rules.
  87  * The documentation of each field explains how it operates.
  88  *
  89  * <h3>Specification for implementors</h3>
  90  * This is a final, immutable and thread-safe enum.
  91  *
  92  * @since 1.8
  93  */
  94 public enum ChronoField implements TemporalField {
  95 
  96     /**


 504         this.rangeUnit = rangeUnit;
 505         this.range = range;
 506     }
 507 
 508     //-----------------------------------------------------------------------
 509     @Override
 510     public String getName() {
 511         return name;
 512     }
 513 
 514     @Override
 515     public TemporalUnit getBaseUnit() {
 516         return baseUnit;
 517     }
 518 
 519     @Override
 520     public TemporalUnit getRangeUnit() {
 521         return rangeUnit;
 522     }
 523 

















 524     @Override
 525     public ValueRange range() {
 526         return range;
 527     }
 528 
 529     //-----------------------------------------------------------------------
 530     /**
 531      * Checks if this field represents a component of a date.
 532      *
 533      * @return true if it is a component of a date
 534      */
 535     public boolean isDateField() {
 536         return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();
 537     }
 538 
 539     /**
 540      * Checks if this field represents a component of a time.
 541      *
 542      * @return true if it is a component of a time
 543      */
 544     public boolean isTimeField() {
 545         return ordinal() < DAY_OF_WEEK.ordinal();
 546     }
 547 
 548     //-----------------------------------------------------------------------
 549     /**
 550      * Checks that the specified value is valid for this field.
 551      * <p>
 552      * This validates that the value is within the outer range of valid values
 553      * returned by {@link #range()}.





 554      *
 555      * @param value  the value to check
 556      * @return the value that was passed in
 557      */
 558     public long checkValidValue(long value) {
 559         return range().checkValidValue(value, this);
 560     }
 561 
 562     /**
 563      * Checks that the specified value is valid and fits in an {@code int}.
 564      * <p>
 565      * This validates that the value is within the outer range of valid values
 566      * returned by {@link #range()}.
 567      * It also checks that all valid values are within the bounds of an {@code int}.





 568      *
 569      * @param value  the value to check
 570      * @return the value that was passed in
 571      */
 572     public int checkValidIntValue(long value) {
 573         return range().checkValidIntValue(value, this);
 574     }
 575 
 576     //-----------------------------------------------------------------------
 577     @Override
 578     public boolean doIsSupported(TemporalAccessor temporal) {
 579         return temporal.isSupported(this);
 580     }
 581 
 582     @Override
 583     public ValueRange doRange(TemporalAccessor temporal) {
 584         return temporal.range(this);
 585     }
 586 
 587     @Override
 588     public long doGet(TemporalAccessor temporal) {
 589         return temporal.getLong(this);
 590     }
 591 
 592     @SuppressWarnings("unchecked")
 593     @Override
 594     public <R extends Temporal> R doWith(R temporal, long newValue) {
 595         return (R) temporal.with(this, newValue);
 596     }
 597 
 598     //-----------------------------------------------------------------------
 599     @Override
 600     public boolean resolve(DateTimeBuilder builder, long value) {
 601         return false;  // resolve implemented in builder
 602     }
 603 
 604     //-----------------------------------------------------------------------
 605     @Override
 606     public String toString() {
 607         return getName();
 608     }
 609 
 610 }


  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.temporal;
  58 
  59 import static java.time.temporal.ChronoUnit.DAYS;
  60 import static java.time.temporal.ChronoUnit.ERAS;
  61 import static java.time.temporal.ChronoUnit.FOREVER;
  62 import static java.time.temporal.ChronoUnit.HALF_DAYS;
  63 import static java.time.temporal.ChronoUnit.HOURS;
  64 import static java.time.temporal.ChronoUnit.MICROS;
  65 import static java.time.temporal.ChronoUnit.MILLIS;
  66 import static java.time.temporal.ChronoUnit.MINUTES;
  67 import static java.time.temporal.ChronoUnit.MONTHS;
  68 import static java.time.temporal.ChronoUnit.NANOS;
  69 import static java.time.temporal.ChronoUnit.SECONDS;
  70 import static java.time.temporal.ChronoUnit.WEEKS;
  71 import static java.time.temporal.ChronoUnit.YEARS;
  72 
  73 import java.time.DayOfWeek;
  74 import java.time.Instant;
  75 import java.time.Year;
  76 import java.time.ZoneOffset;
  77 import java.time.chrono.ChronoLocalDate;
  78 import java.time.chrono.Chronology;
  79 
  80 /**
  81  * A standard set of fields.
  82  * <p>
  83  * This set of fields provide field-based access to manipulate a date, time or date-time.
  84  * The standard set of fields can be extended by implementing {@link TemporalField}.
  85  * <p>
  86  * These fields are intended to be applicable in multiple calendar systems.
  87  * For example, most non-ISO calendar systems define dates as a year, month and day,
  88  * just with slightly different rules.
  89  * The documentation of each field explains how it operates.
  90  *
  91  * <h3>Specification for implementors</h3>
  92  * This is a final, immutable and thread-safe enum.
  93  *
  94  * @since 1.8
  95  */
  96 public enum ChronoField implements TemporalField {
  97 
  98     /**


 506         this.rangeUnit = rangeUnit;
 507         this.range = range;
 508     }
 509 
 510     //-----------------------------------------------------------------------
 511     @Override
 512     public String getName() {
 513         return name;
 514     }
 515 
 516     @Override
 517     public TemporalUnit getBaseUnit() {
 518         return baseUnit;
 519     }
 520 
 521     @Override
 522     public TemporalUnit getRangeUnit() {
 523         return rangeUnit;
 524     }
 525 
 526     /**
 527      * Gets the range of valid values for the field.
 528      * <p>
 529      * All fields can be expressed as a {@code long} integer.
 530      * This method returns an object that describes the valid range for that value.
 531      * <p>
 532      * This method returns the range of the field in the ISO-8601 calendar system.
 533      * This range may be incorrect for other calendar systems.
 534      * Use {@link Chronology#range(ChronoField)} to access the correct range
 535      * for a different calendar system.
 536      * <p>
 537      * Note that the result only describes the minimum and maximum valid values
 538      * and it is important not to read too much into them. For example, there
 539      * could be values within the range that are invalid for the field.
 540      *
 541      * @return the range of valid values for the field, not null
 542      */
 543     @Override
 544     public ValueRange range() {
 545         return range;
 546     }
 547 
 548     //-----------------------------------------------------------------------
 549     /**
 550      * Checks if this field represents a component of a date.
 551      *
 552      * @return true if it is a component of a date
 553      */
 554     public boolean isDateField() {
 555         return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();
 556     }
 557 
 558     /**
 559      * Checks if this field represents a component of a time.
 560      *
 561      * @return true if it is a component of a time
 562      */
 563     public boolean isTimeField() {
 564         return ordinal() < DAY_OF_WEEK.ordinal();
 565     }
 566 
 567     //-----------------------------------------------------------------------
 568     /**
 569      * Checks that the specified value is valid for this field.
 570      * <p>
 571      * This validates that the value is within the outer range of valid values
 572      * returned by {@link #range()}.
 573      * <p>
 574      * This method checks against the range of the field in the ISO-8601 calendar system.
 575      * This range may be incorrect for other calendar systems.
 576      * Use {@link Chronology#range(ChronoField)} to access the correct range
 577      * for a different calendar system.
 578      *
 579      * @param value  the value to check
 580      * @return the value that was passed in
 581      */
 582     public long checkValidValue(long value) {
 583         return range().checkValidValue(value, this);
 584     }
 585 
 586     /**
 587      * Checks that the specified value is valid and fits in an {@code int}.
 588      * <p>
 589      * This validates that the value is within the outer range of valid values
 590      * returned by {@link #range()}.
 591      * It also checks that all valid values are within the bounds of an {@code int}.
 592      * <p>
 593      * This method checks against the range of the field in the ISO-8601 calendar system.
 594      * This range may be incorrect for other calendar systems.
 595      * Use {@link Chronology#range(ChronoField)} to access the correct range
 596      * for a different calendar system.
 597      *
 598      * @param value  the value to check
 599      * @return the value that was passed in
 600      */
 601     public int checkValidIntValue(long value) {
 602         return range().checkValidIntValue(value, this);
 603     }
 604 
 605     //-----------------------------------------------------------------------
 606     @Override
 607     public boolean isSupportedBy(TemporalAccessor temporal) {
 608         return temporal.isSupported(this);
 609     }
 610 
 611     @Override
 612     public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
 613         return temporal.range(this);
 614     }
 615 
 616     @Override
 617     public long getFrom(TemporalAccessor temporal) {
 618         return temporal.getLong(this);
 619     }
 620 
 621     @SuppressWarnings("unchecked")
 622     @Override
 623     public <R extends Temporal> R adjustInto(R temporal, long newValue) {
 624         return (R) temporal.with(this, newValue);
 625     }
 626 
 627     //-----------------------------------------------------------------------
 628     @Override






 629     public String toString() {
 630         return getName();
 631     }
 632 
 633 }