src/share/classes/java/time/temporal/TemporalUnit.java
Print this page
*** 141,154 ****
* {@link Temporal#plus(long, TemporalUnit)}.
*
* @param temporal the temporal object to check, not null
* @return true if the unit is supported
*/
! public default boolean isSupportedBy(Temporal temporal) {
try {
temporal.plus(1, this);
return true;
} catch (RuntimeException ex) {
try {
temporal.plus(-1, this);
return true;
} catch (RuntimeException ex2) {
--- 141,156 ----
* {@link Temporal#plus(long, TemporalUnit)}.
*
* @param temporal the temporal object to check, not null
* @return true if the unit is supported
*/
! default boolean isSupportedBy(Temporal temporal) {
try {
temporal.plus(1, this);
return true;
+ } catch (UnsupportedTemporalTypeException ex) {
+ return false;
} catch (RuntimeException ex) {
try {
temporal.plus(-1, this);
return true;
} catch (RuntimeException ex2) {
*** 176,196 ****
* It is recommended to use the second approach, {@code plus(TemporalUnit)},
* as it is a lot clearer to read in code.
* <p>
* Implementations should perform any queries or calculations using the units
* available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
! * If the unit is not supported a {@code DateTimeException} must be thrown.
* <p>
* Implementations must not alter the specified temporal object.
* Instead, an adjusted copy of the original must be returned.
* This provides equivalent, safe behavior for immutable and mutable implementations.
*
* @param <R> the type of the Temporal object
* @param temporal the temporal object to adjust, not null
* @param amount the amount of this unit to add, positive or negative
* @return the adjusted temporal object, not null
* @throws DateTimeException if the period cannot be added
*/
<R extends Temporal> R addTo(R temporal, long amount);
//-----------------------------------------------------------------------
/**
--- 178,199 ----
* It is recommended to use the second approach, {@code plus(TemporalUnit)},
* as it is a lot clearer to read in code.
* <p>
* Implementations should perform any queries or calculations using the units
* available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
! * If the unit is not supported an {@code UnsupportedTemporalTypeException} must be thrown.
* <p>
* Implementations must not alter the specified temporal object.
* Instead, an adjusted copy of the original must be returned.
* This provides equivalent, safe behavior for immutable and mutable implementations.
*
* @param <R> the type of the Temporal object
* @param temporal the temporal object to adjust, not null
* @param amount the amount of this unit to add, positive or negative
* @return the adjusted temporal object, not null
* @throws DateTimeException if the period cannot be added
+ * @throws UnsupportedTemporalTypeException if the unit is not supported by the temporal
*/
<R extends Temporal> R addTo(R temporal, long amount);
//-----------------------------------------------------------------------
/**
*** 212,223 ****
* There are two equivalent ways of using this method.
* The first is to invoke this method directly.
* The second is to use {@link Temporal#periodUntil(Temporal, TemporalUnit)}:
* <pre>
* // these two lines are equivalent
! * temporal = thisUnit.between(start, end);
! * temporal = start.periodUntil(end, thisUnit);
* </pre>
* The choice should be made based on which makes the code more readable.
* <p>
* For example, this method allows the number of days between two dates to
* be calculated:
--- 215,226 ----
* There are two equivalent ways of using this method.
* The first is to invoke this method directly.
* The second is to use {@link Temporal#periodUntil(Temporal, TemporalUnit)}:
* <pre>
* // these two lines are equivalent
! * between = thisUnit.between(start, end);
! * between = start.periodUntil(end, thisUnit);
* </pre>
* The choice should be made based on which makes the code more readable.
* <p>
* For example, this method allows the number of days between two dates to
* be calculated:
*** 227,244 ****
* long daysBetween = start.periodUntil(end, DAYS);
* </pre>
* <p>
* Implementations should perform any queries or calculations using the units
* available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
! * If the unit is not supported a {@code DateTimeException} must be thrown.
* Implementations must not alter the specified temporal objects.
*
* @param temporal1 the base temporal object, not null
* @param temporal2 the other temporal object, not null
! * @return the period between datetime1 and datetime2 in terms of this unit;
! * positive if datetime2 is later than datetime1, negative if earlier
* @throws DateTimeException if the period cannot be calculated
* @throws ArithmeticException if numeric overflow occurs
*/
long between(Temporal temporal1, Temporal temporal2);
//-----------------------------------------------------------------------
--- 230,248 ----
* long daysBetween = start.periodUntil(end, DAYS);
* </pre>
* <p>
* Implementations should perform any queries or calculations using the units
* available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
! * If the unit is not supported an {@code UnsupportedTemporalTypeException} must be thrown.
* Implementations must not alter the specified temporal objects.
*
* @param temporal1 the base temporal object, not null
* @param temporal2 the other temporal object, not null
! * @return the period between temporal1 and temporal2 in terms of this unit;
! * positive if temporal2 is later than temporal1, negative if earlier
* @throws DateTimeException if the period cannot be calculated
+ * @throws UnsupportedTemporalTypeException if the unit is not supported by the temporal
* @throws ArithmeticException if numeric overflow occurs
*/
long between(Temporal temporal1, Temporal temporal2);
//-----------------------------------------------------------------------