62 package java.time; |
62 package java.time; |
63 |
63 |
64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
65 import static java.time.temporal.ChronoUnit.MONTHS; |
65 import static java.time.temporal.ChronoUnit.MONTHS; |
66 |
66 |
|
67 import java.time.chrono.Chronology; |
|
68 import java.time.chrono.IsoChronology; |
67 import java.time.format.DateTimeFormatterBuilder; |
69 import java.time.format.DateTimeFormatterBuilder; |
68 import java.time.format.TextStyle; |
70 import java.time.format.TextStyle; |
69 import java.time.temporal.Chrono; |
|
70 import java.time.temporal.ChronoField; |
71 import java.time.temporal.ChronoField; |
71 import java.time.temporal.ISOChrono; |
|
72 import java.time.temporal.Queries; |
72 import java.time.temporal.Queries; |
73 import java.time.temporal.Temporal; |
73 import java.time.temporal.Temporal; |
74 import java.time.temporal.TemporalAccessor; |
74 import java.time.temporal.TemporalAccessor; |
75 import java.time.temporal.TemporalAdjuster; |
75 import java.time.temporal.TemporalAdjuster; |
76 import java.time.temporal.TemporalField; |
76 import java.time.temporal.TemporalField; |
190 |
190 |
191 //----------------------------------------------------------------------- |
191 //----------------------------------------------------------------------- |
192 /** |
192 /** |
193 * Obtains an instance of {@code Month} from a temporal object. |
193 * Obtains an instance of {@code Month} from a temporal object. |
194 * <p> |
194 * <p> |
195 * A {@code TemporalAccessor} represents some form of date and time information. |
195 * This obtains a month based on the specified temporal. |
196 * This factory converts the arbitrary temporal object to an instance of {@code Month}. |
196 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
197 * which this factory converts to an instance of {@code Month}. |
197 * <p> |
198 * <p> |
198 * The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} field. |
199 * The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} field. |
199 * The extraction is only permitted if the temporal object has an ISO |
200 * The extraction is only permitted if the temporal object has an ISO |
200 * chronology, or can be converted to a {@code LocalDate}. |
201 * chronology, or can be converted to a {@code LocalDate}. |
201 * <p> |
202 * <p> |
209 public static Month from(TemporalAccessor temporal) { |
210 public static Month from(TemporalAccessor temporal) { |
210 if (temporal instanceof Month) { |
211 if (temporal instanceof Month) { |
211 return (Month) temporal; |
212 return (Month) temporal; |
212 } |
213 } |
213 try { |
214 try { |
214 if (ISOChrono.INSTANCE.equals(Chrono.from(temporal)) == false) { |
215 if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) { |
215 temporal = LocalDate.from(temporal); |
216 temporal = LocalDate.from(temporal); |
216 } |
217 } |
217 return of(temporal.get(MONTH_OF_YEAR)); |
218 return of(temporal.get(MONTH_OF_YEAR)); |
218 } catch (DateTimeException ex) { |
219 } catch (DateTimeException ex) { |
219 throw new DateTimeException("Unable to obtain Month from TemporalAccessor: " + temporal.getClass(), ex); |
220 throw new DateTimeException("Unable to obtain Month from TemporalAccessor: " + temporal.getClass(), ex); |
235 |
236 |
236 //----------------------------------------------------------------------- |
237 //----------------------------------------------------------------------- |
237 /** |
238 /** |
238 * Gets the textual representation, such as 'Jan' or 'December'. |
239 * Gets the textual representation, such as 'Jan' or 'December'. |
239 * <p> |
240 * <p> |
240 * This returns the textual name used to identify the month-of-year. |
241 * This returns the textual name used to identify the month-of-year, |
241 * The parameters control the length of the returned text and the locale. |
242 * suitable for presentation to the user. |
|
243 * The parameters control the style of the returned text and the locale. |
242 * <p> |
244 * <p> |
243 * If no textual mapping is found then the {@link #getValue() numeric value} is returned. |
245 * If no textual mapping is found then the {@link #getValue() numeric value} is returned. |
244 * |
246 * |
245 * @param style the length of the text required, not null |
247 * @param style the length of the text required, not null |
246 * @param locale the locale to use, not null |
248 * @param locale the locale to use, not null |
247 * @return the text value of the month-of-year, not null |
249 * @return the text value of the month-of-year, not null |
248 */ |
250 */ |
249 public String getText(TextStyle style, Locale locale) { |
251 public String getDisplayName(TextStyle style, Locale locale) { |
250 return new DateTimeFormatterBuilder().appendText(MONTH_OF_YEAR, style).toFormatter(locale).print(this); |
252 return new DateTimeFormatterBuilder().appendText(MONTH_OF_YEAR, style).toFormatter(locale).format(this); |
251 } |
253 } |
252 |
254 |
253 //----------------------------------------------------------------------- |
255 //----------------------------------------------------------------------- |
254 /** |
256 /** |
255 * Checks if the specified field is supported. |
257 * Checks if the specified field is supported. |
261 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then |
263 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then |
262 * this method returns true. |
264 * this method returns true. |
263 * All other {@code ChronoField} instances will return false. |
265 * All other {@code ChronoField} instances will return false. |
264 * <p> |
266 * <p> |
265 * If the field is not a {@code ChronoField}, then the result of this method |
267 * If the field is not a {@code ChronoField}, then the result of this method |
266 * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)} |
268 * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
267 * passing {@code this} as the argument. |
269 * passing {@code this} as the argument. |
268 * Whether the field is supported is determined by the field. |
270 * Whether the field is supported is determined by the field. |
269 * |
271 * |
270 * @param field the field to check, null returns false |
272 * @param field the field to check, null returns false |
271 * @return true if the field is supported on this month-of-year, false if not |
273 * @return true if the field is supported on this month-of-year, false if not |
273 @Override |
275 @Override |
274 public boolean isSupported(TemporalField field) { |
276 public boolean isSupported(TemporalField field) { |
275 if (field instanceof ChronoField) { |
277 if (field instanceof ChronoField) { |
276 return field == MONTH_OF_YEAR; |
278 return field == MONTH_OF_YEAR; |
277 } |
279 } |
278 return field != null && field.doIsSupported(this); |
280 return field != null && field.isSupportedBy(this); |
279 } |
281 } |
280 |
282 |
281 /** |
283 /** |
282 * Gets the range of valid values for the specified field. |
284 * Gets the range of valid values for the specified field. |
283 * <p> |
285 * <p> |
289 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the |
291 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the |
290 * range of the month-of-year, from 1 to 12, will be returned. |
292 * range of the month-of-year, from 1 to 12, will be returned. |
291 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
293 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
292 * <p> |
294 * <p> |
293 * If the field is not a {@code ChronoField}, then the result of this method |
295 * If the field is not a {@code ChronoField}, then the result of this method |
294 * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)} |
296 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
295 * passing {@code this} as the argument. |
297 * passing {@code this} as the argument. |
296 * Whether the range can be obtained is determined by the field. |
298 * Whether the range can be obtained is determined by the field. |
297 * |
299 * |
298 * @param field the field to query the range for, not null |
300 * @param field the field to query the range for, not null |
299 * @return the range of valid values for the field, not null |
301 * @return the range of valid values for the field, not null |
318 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the |
320 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the |
319 * value of the month-of-year, from 1 to 12, will be returned. |
321 * value of the month-of-year, from 1 to 12, will be returned. |
320 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
322 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
321 * <p> |
323 * <p> |
322 * If the field is not a {@code ChronoField}, then the result of this method |
324 * If the field is not a {@code ChronoField}, then the result of this method |
323 * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)} |
325 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
324 * passing {@code this} as the argument. Whether the value can be obtained, |
326 * passing {@code this} as the argument. Whether the value can be obtained, |
325 * and what the value represents, is determined by the field. |
327 * and what the value represents, is determined by the field. |
326 * |
328 * |
327 * @param field the field to get, not null |
329 * @param field the field to get, not null |
328 * @return the value for the field, within the valid range of values |
330 * @return the value for the field, within the valid range of values |
329 * @throws DateTimeException if a value for the field cannot be obtained |
331 * @throws DateTimeException if a value for the field cannot be obtained |
330 * @throws DateTimeException if the range of valid values for the field exceeds an {@code int} |
|
331 * @throws DateTimeException if the value is outside the range of valid values for the field |
|
332 * @throws ArithmeticException if numeric overflow occurs |
332 * @throws ArithmeticException if numeric overflow occurs |
333 */ |
333 */ |
334 @Override |
334 @Override |
335 public int get(TemporalField field) { |
335 public int get(TemporalField field) { |
336 if (field == MONTH_OF_YEAR) { |
336 if (field == MONTH_OF_YEAR) { |
349 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the |
349 * If the field is {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} then the |
350 * value of the month-of-year, from 1 to 12, will be returned. |
350 * value of the month-of-year, from 1 to 12, will be returned. |
351 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
351 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
352 * <p> |
352 * <p> |
353 * If the field is not a {@code ChronoField}, then the result of this method |
353 * If the field is not a {@code ChronoField}, then the result of this method |
354 * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)} |
354 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
355 * passing {@code this} as the argument. Whether the value can be obtained, |
355 * passing {@code this} as the argument. Whether the value can be obtained, |
356 * and what the value represents, is determined by the field. |
356 * and what the value represents, is determined by the field. |
357 * |
357 * |
358 * @param field the field to get, not null |
358 * @param field the field to get, not null |
359 * @return the value for the field |
359 * @return the value for the field |
365 if (field == MONTH_OF_YEAR) { |
365 if (field == MONTH_OF_YEAR) { |
366 return getValue(); |
366 return getValue(); |
367 } else if (field instanceof ChronoField) { |
367 } else if (field instanceof ChronoField) { |
368 throw new DateTimeException("Unsupported field: " + field.getName()); |
368 throw new DateTimeException("Unsupported field: " + field.getName()); |
369 } |
369 } |
370 return field.doGet(this); |
370 return field.getFrom(this); |
371 } |
371 } |
372 |
372 |
373 //----------------------------------------------------------------------- |
373 //----------------------------------------------------------------------- |
374 /** |
374 /** |
375 * Returns the month-of-year that is the specified number of quarters after this one. |
375 * Returns the month-of-year that is the specified number of quarters after this one. |
552 * @throws ArithmeticException if numeric overflow occurs (defined by the query) |
552 * @throws ArithmeticException if numeric overflow occurs (defined by the query) |
553 */ |
553 */ |
554 @SuppressWarnings("unchecked") |
554 @SuppressWarnings("unchecked") |
555 @Override |
555 @Override |
556 public <R> R query(TemporalQuery<R> query) { |
556 public <R> R query(TemporalQuery<R> query) { |
557 if (query == Queries.chrono()) { |
557 if (query == Queries.chronology()) { |
558 return (R) ISOChrono.INSTANCE; |
558 return (R) IsoChronology.INSTANCE; |
559 } else if (query == Queries.precision()) { |
559 } else if (query == Queries.precision()) { |
560 return (R) MONTHS; |
560 return (R) MONTHS; |
561 } |
561 } |
562 return TemporalAccessor.super.query(query); |
562 return TemporalAccessor.super.query(query); |
563 } |
563 } |
597 * @throws DateTimeException if unable to make the adjustment |
597 * @throws DateTimeException if unable to make the adjustment |
598 * @throws ArithmeticException if numeric overflow occurs |
598 * @throws ArithmeticException if numeric overflow occurs |
599 */ |
599 */ |
600 @Override |
600 @Override |
601 public Temporal adjustInto(Temporal temporal) { |
601 public Temporal adjustInto(Temporal temporal) { |
602 if (Chrono.from(temporal).equals(ISOChrono.INSTANCE) == false) { |
602 if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) { |
603 throw new DateTimeException("Adjustment only supported on ISO date-time"); |
603 throw new DateTimeException("Adjustment only supported on ISO date-time"); |
604 } |
604 } |
605 return temporal.with(MONTH_OF_YEAR, getValue()); |
605 return temporal.with(MONTH_OF_YEAR, getValue()); |
606 } |
606 } |
607 |
607 |