61 */ |
61 */ |
62 package java.time.chrono; |
62 package java.time.chrono; |
63 |
63 |
64 import static java.time.temporal.ChronoField.EPOCH_DAY; |
64 import static java.time.temporal.ChronoField.EPOCH_DAY; |
65 import static java.time.temporal.ChronoField.NANO_OF_DAY; |
65 import static java.time.temporal.ChronoField.NANO_OF_DAY; |
|
66 import static java.time.temporal.ChronoUnit.FOREVER; |
66 import static java.time.temporal.ChronoUnit.NANOS; |
67 import static java.time.temporal.ChronoUnit.NANOS; |
67 |
68 |
68 import java.time.DateTimeException; |
69 import java.time.DateTimeException; |
69 import java.time.Instant; |
70 import java.time.Instant; |
70 import java.time.LocalDateTime; |
71 import java.time.LocalDateTime; |
71 import java.time.LocalTime; |
72 import java.time.LocalTime; |
72 import java.time.ZoneId; |
73 import java.time.ZoneId; |
73 import java.time.ZoneOffset; |
74 import java.time.ZoneOffset; |
74 import java.time.format.DateTimeFormatter; |
75 import java.time.format.DateTimeFormatter; |
75 import java.time.temporal.ChronoField; |
76 import java.time.temporal.ChronoField; |
|
77 import java.time.temporal.ChronoUnit; |
76 import java.time.temporal.Temporal; |
78 import java.time.temporal.Temporal; |
77 import java.time.temporal.TemporalAccessor; |
79 import java.time.temporal.TemporalAccessor; |
78 import java.time.temporal.TemporalAdjuster; |
80 import java.time.temporal.TemporalAdjuster; |
79 import java.time.temporal.TemporalAmount; |
81 import java.time.temporal.TemporalAmount; |
80 import java.time.temporal.TemporalField; |
82 import java.time.temporal.TemporalField; |
112 * Subclasses should be Serializable wherever possible. |
114 * Subclasses should be Serializable wherever possible. |
113 * |
115 * |
114 * @param <D> the concrete type for the date of this date-time |
116 * @param <D> the concrete type for the date of this date-time |
115 * @since 1.8 |
117 * @since 1.8 |
116 */ |
118 */ |
117 public interface ChronoLocalDateTime<D extends ChronoLocalDate<D>> |
119 public interface ChronoLocalDateTime<D extends ChronoLocalDate> |
118 extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDateTime<?>> { |
120 extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDateTime<?>> { |
119 |
121 |
120 /** |
122 /** |
121 * Gets a comparator that compares {@code ChronoLocalDateTime} in |
123 * Gets a comparator that compares {@code ChronoLocalDateTime} in |
122 * time-line order ignoring the chronology. |
124 * time-line order ignoring the chronology. |
189 * |
191 * |
190 * @return the time part of this date-time, not null |
192 * @return the time part of this date-time, not null |
191 */ |
193 */ |
192 LocalTime toLocalTime(); |
194 LocalTime toLocalTime(); |
193 |
195 |
194 @Override // Override to provide javadoc |
196 /** |
|
197 * Checks if the specified field is supported. |
|
198 * <p> |
|
199 * This checks if the specified field can be queried on this date-time. |
|
200 * If false, then calling the {@link #range(TemporalField) range}, |
|
201 * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} |
|
202 * methods will throw an exception. |
|
203 * <p> |
|
204 * The set of supported fields is defined by the chronology and normally includes |
|
205 * all {@code ChronoField} date and time fields. |
|
206 * <p> |
|
207 * If the field is not a {@code ChronoField}, then the result of this method |
|
208 * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
|
209 * passing {@code this} as the argument. |
|
210 * Whether the field is supported is determined by the field. |
|
211 * |
|
212 * @param field the field to check, null returns false |
|
213 * @return true if the field can be queried, false if not |
|
214 */ |
|
215 @Override |
195 boolean isSupported(TemporalField field); |
216 boolean isSupported(TemporalField field); |
|
217 |
|
218 /** |
|
219 * Checks if the specified unit is supported. |
|
220 * <p> |
|
221 * This checks if the specified unit can be added to or subtracted from this date-time. |
|
222 * If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
223 * {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
224 * <p> |
|
225 * The set of supported units is defined by the chronology and normally includes |
|
226 * all {@code ChronoUnit} units except {@code FOREVER}. |
|
227 * <p> |
|
228 * If the unit is not a {@code ChronoUnit}, then the result of this method |
|
229 * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
230 * passing {@code this} as the argument. |
|
231 * Whether the unit is supported is determined by the unit. |
|
232 * |
|
233 * @param unit the unit to check, null returns false |
|
234 * @return true if the unit can be added/subtracted, false if not |
|
235 */ |
|
236 @Override |
|
237 default boolean isSupported(TemporalUnit unit) { |
|
238 if (unit instanceof ChronoUnit) { |
|
239 return unit != FOREVER; |
|
240 } |
|
241 return unit != null && unit.isSupportedBy(this); |
|
242 } |
196 |
243 |
197 //----------------------------------------------------------------------- |
244 //----------------------------------------------------------------------- |
198 // override for covariant return type |
245 // override for covariant return type |
199 /** |
246 /** |
200 * {@inheritDoc} |
247 * {@inheritDoc} |
201 * @throws DateTimeException {@inheritDoc} |
248 * @throws DateTimeException {@inheritDoc} |
202 * @throws ArithmeticException {@inheritDoc} |
249 * @throws ArithmeticException {@inheritDoc} |
203 */ |
250 */ |
204 @Override |
251 @Override |
205 default ChronoLocalDateTime<D> with(TemporalAdjuster adjuster) { |
252 default ChronoLocalDateTime<D> with(TemporalAdjuster adjuster) { |
206 return (ChronoLocalDateTime<D>)(toLocalDate().getChronology().ensureChronoLocalDateTime(Temporal.super.with(adjuster))); |
253 return ChronoLocalDateTimeImpl.ensureValid(toLocalDate().getChronology(), Temporal.super.with(adjuster)); |
207 } |
254 } |
208 |
255 |
209 /** |
256 /** |
210 * {@inheritDoc} |
257 * {@inheritDoc} |
211 * @throws DateTimeException {@inheritDoc} |
258 * @throws DateTimeException {@inheritDoc} |
237 * @throws DateTimeException {@inheritDoc} |
284 * @throws DateTimeException {@inheritDoc} |
238 * @throws ArithmeticException {@inheritDoc} |
285 * @throws ArithmeticException {@inheritDoc} |
239 */ |
286 */ |
240 @Override |
287 @Override |
241 default ChronoLocalDateTime<D> minus(TemporalAmount amount) { |
288 default ChronoLocalDateTime<D> minus(TemporalAmount amount) { |
242 return (ChronoLocalDateTime<D>)(toLocalDate().getChronology().ensureChronoLocalDateTime(Temporal.super.minus(amount))); |
289 return ChronoLocalDateTimeImpl.ensureValid(toLocalDate().getChronology(), Temporal.super.minus(amount)); |
243 } |
290 } |
244 |
291 |
245 /** |
292 /** |
246 * {@inheritDoc} |
293 * {@inheritDoc} |
247 * @throws DateTimeException {@inheritDoc} |
294 * @throws DateTimeException {@inheritDoc} |
248 * @throws ArithmeticException {@inheritDoc} |
295 * @throws ArithmeticException {@inheritDoc} |
249 */ |
296 */ |
250 @Override |
297 @Override |
251 default ChronoLocalDateTime<D> minus(long amountToSubtract, TemporalUnit unit) { |
298 default ChronoLocalDateTime<D> minus(long amountToSubtract, TemporalUnit unit) { |
252 return (ChronoLocalDateTime<D>)(toLocalDate().getChronology().ensureChronoLocalDateTime(Temporal.super.minus(amountToSubtract, unit))); |
299 return ChronoLocalDateTimeImpl.ensureValid(toLocalDate().getChronology(), Temporal.super.minus(amountToSubtract, unit)); |
253 } |
300 } |
254 |
301 |
255 //----------------------------------------------------------------------- |
302 //----------------------------------------------------------------------- |
256 /** |
303 /** |
257 * Queries this date-time using the specified query. |
304 * Queries this date-time using the specified query. |