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 * |