jdk/src/share/classes/java/time/temporal/TemporalUnit.java
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
equal deleted inserted replaced
15657:c588664d547e 15658:55b829ca2334
    73  * <p>
    73  * <p>
    74  * An instance of this interface represents the unit itself, rather than an amount of the unit.
    74  * An instance of this interface represents the unit itself, rather than an amount of the unit.
    75  * See {@link Period} for a class that represents an amount in terms of the common units.
    75  * See {@link Period} for a class that represents an amount in terms of the common units.
    76  * <p>
    76  * <p>
    77  * The most commonly used units are defined in {@link ChronoUnit}.
    77  * The most commonly used units are defined in {@link ChronoUnit}.
    78  * Further units are supplied in {@link ISOFields}.
    78  * Further units are supplied in {@link IsoFields}.
    79  * Units can also be written by application code by implementing this interface.
    79  * Units can also be written by application code by implementing this interface.
    80  * <p>
    80  * <p>
    81  * The unit works using double dispatch. Client code calls methods on a date-time like
    81  * The unit works using double dispatch. Client code calls methods on a date-time like
    82  * {@code LocalDateTime} which check if the unit is a {@code ChronoUnit}.
    82  * {@code LocalDateTime} which check if the unit is a {@code ChronoUnit}.
    83  * If it is, then the date-time must handle it.
    83  * If it is, then the date-time must handle it.
   103 
   103 
   104     /**
   104     /**
   105      * Gets the duration of this unit, which may be an estimate.
   105      * Gets the duration of this unit, which may be an estimate.
   106      * <p>
   106      * <p>
   107      * All units return a duration measured in standard nanoseconds from this method.
   107      * All units return a duration measured in standard nanoseconds from this method.
       
   108      * The duration will be positive and non-zero.
   108      * For example, an hour has a duration of {@code 60 * 60 * 1,000,000,000ns}.
   109      * For example, an hour has a duration of {@code 60 * 60 * 1,000,000,000ns}.
   109      * <p>
   110      * <p>
   110      * Some units may return an accurate duration while others return an estimate.
   111      * Some units may return an accurate duration while others return an estimate.
   111      * For example, days have an estimated duration due to the possibility of
   112      * For example, days have an estimated duration due to the possibility of
   112      * daylight saving time changes.
   113      * daylight saving time changes.
   140      * {@link Temporal#plus(long, TemporalUnit)}.
   141      * {@link Temporal#plus(long, TemporalUnit)}.
   141      *
   142      *
   142      * @param temporal  the temporal object to check, not null
   143      * @param temporal  the temporal object to check, not null
   143      * @return true if the unit is supported
   144      * @return true if the unit is supported
   144      */
   145      */
   145     public default boolean isSupported(Temporal temporal) {
   146     public default boolean isSupportedBy(Temporal temporal) {
   146         try {
   147         try {
   147             temporal.plus(1, this);
   148             temporal.plus(1, this);
   148             return true;
   149             return true;
   149         } catch (RuntimeException ex) {
   150         } catch (RuntimeException ex) {
   150             try {
   151             try {
   167      * There are two equivalent ways of using this method.
   168      * There are two equivalent ways of using this method.
   168      * The first is to invoke this method directly.
   169      * The first is to invoke this method directly.
   169      * The second is to use {@link Temporal#plus(long, TemporalUnit)}:
   170      * The second is to use {@link Temporal#plus(long, TemporalUnit)}:
   170      * <pre>
   171      * <pre>
   171      *   // these two lines are equivalent, but the second approach is recommended
   172      *   // these two lines are equivalent, but the second approach is recommended
   172      *   temporal = thisUnit.doPlus(temporal);
   173      *   temporal = thisUnit.addTo(temporal);
   173      *   temporal = temporal.plus(thisUnit);
   174      *   temporal = temporal.plus(thisUnit);
   174      * </pre>
   175      * </pre>
   175      * It is recommended to use the second approach, {@code plus(TemporalUnit)},
   176      * It is recommended to use the second approach, {@code plus(TemporalUnit)},
   176      * as it is a lot clearer to read in code.
   177      * as it is a lot clearer to read in code.
   177      * <p>
   178      * <p>
   178      * Implementations should perform any queries or calculations using the units
   179      * Implementations should perform any queries or calculations using the units
   179      * available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
   180      * available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
   180      * If the field is not supported a {@code DateTimeException} must be thrown.
   181      * If the unit is not supported a {@code DateTimeException} must be thrown.
   181      * <p>
   182      * <p>
   182      * Implementations must not alter the specified temporal object.
   183      * Implementations must not alter the specified temporal object.
   183      * Instead, an adjusted copy of the original must be returned.
   184      * Instead, an adjusted copy of the original must be returned.
   184      * This provides equivalent, safe behavior for immutable and mutable implementations.
   185      * This provides equivalent, safe behavior for immutable and mutable implementations.
   185      *
   186      *
   186      * @param <R>  the type of the Temporal object
   187      * @param <R>  the type of the Temporal object
   187      * @param dateTime  the temporal object to adjust, not null
   188      * @param temporal  the temporal object to adjust, not null
   188      * @param periodToAdd  the period of this unit to add, positive or negative
   189      * @param amount  the amount of this unit to add, positive or negative
   189      * @return the adjusted temporal object, not null
   190      * @return the adjusted temporal object, not null
   190      * @throws DateTimeException if the period cannot be added
   191      * @throws DateTimeException if the period cannot be added
   191      */
   192      */
   192     <R extends Temporal> R doPlus(R dateTime, long periodToAdd);
   193     <R extends Temporal> R addTo(R temporal, long amount);
   193 
   194 
   194     //-----------------------------------------------------------------------
   195     //-----------------------------------------------------------------------
   195     /**
   196     /**
   196      * Calculates the period in terms of this unit between two temporal objects of the same type.
   197      * Calculates the period in terms of this unit between two temporal objects
   197      * <p>
   198      * of the same type.
   198      * The period will be positive if the second date-time is after the first, and
   199      * <p>
   199      * negative if the second date-time is before the first.
   200      * This calculates the period between two temporals in terms of this unit.
   200      * Call {@link SimplePeriod#abs() abs()} on the result to ensure that the result
   201      * The start and end points are supplied as temporal objects and must be
   201      * is always positive.
   202      * of the same type.
   202      * <p>
   203      * The result will be negative if the end is before the start.
   203      * The result can be queried for the {@link SimplePeriod#getAmount() amount}, the
   204      * For example, the period in hours between two temporal objects can be
   204      * {@link SimplePeriod#getUnit() unit} and used directly in addition/subtraction:
   205      * calculated using {@code HOURS.between(startTime, endTime)}.
       
   206      * <p>
       
   207      * The calculation returns a whole number, representing the number of
       
   208      * complete units between the two temporals.
       
   209      * For example, the period in hours between the times 11:30 and 13:29
       
   210      * will only be one hour as it is one minute short of two hours.
       
   211      * <p>
       
   212      * There are two equivalent ways of using this method.
       
   213      * The first is to invoke this method directly.
       
   214      * The second is to use {@link Temporal#periodUntil(Temporal, TemporalUnit)}:
   205      * <pre>
   215      * <pre>
   206      *  date = date.minus(MONTHS.between(start, end));
   216      *   // these two lines are equivalent
       
   217      *   temporal = thisUnit.between(start, end);
       
   218      *   temporal = start.periodUntil(end, thisUnit);
   207      * </pre>
   219      * </pre>
   208      *
   220      * The choice should be made based on which makes the code more readable.
   209      * @param <R>  the type of the Temporal object; the two date-times must be of the same type
   221      * <p>
   210      * @param dateTime1  the base temporal object, not null
   222      * For example, this method allows the number of days between two dates to
   211      * @param dateTime2  the other temporal object, not null
   223      * be calculated:
       
   224      * <pre>
       
   225      *  long daysBetween = DAYS.between(start, end);
       
   226      *  // or alternatively
       
   227      *  long daysBetween = start.periodUntil(end, DAYS);
       
   228      * </pre>
       
   229      * <p>
       
   230      * Implementations should perform any queries or calculations using the units
       
   231      * available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
       
   232      * If the unit is not supported a {@code DateTimeException} must be thrown.
       
   233      * Implementations must not alter the specified temporal objects.
       
   234      *
       
   235      * @param temporal1  the base temporal object, not null
       
   236      * @param temporal2  the other temporal object, not null
   212      * @return the period between datetime1 and datetime2 in terms of this unit;
   237      * @return the period between datetime1 and datetime2 in terms of this unit;
   213      *      positive if datetime2 is later than datetime1, not null
   238      *  positive if datetime2 is later than datetime1, negative if earlier
   214      */
   239      * @throws DateTimeException if the period cannot be calculated
   215     <R extends Temporal> SimplePeriod between(R dateTime1, R dateTime2);
   240      * @throws ArithmeticException if numeric overflow occurs
       
   241      */
       
   242     long between(Temporal temporal1, Temporal temporal2);
   216 
   243 
   217     //-----------------------------------------------------------------------
   244     //-----------------------------------------------------------------------
   218     /**
   245     /**
   219      * Outputs this unit as a {@code String} using the name.
   246      * Outputs this unit as a {@code String} using the name.
   220      *
   247      *