src/share/classes/java/time/format/DateTimePrintContext.java

Print this page

        

*** 65,77 **** import static java.time.temporal.ChronoField.INSTANT_SECONDS; import java.time.DateTimeException; import java.time.Instant; import java.time.ZoneId; ! import java.time.temporal.Chrono; import java.time.temporal.ChronoField; ! import java.time.temporal.ChronoLocalDate; import java.time.temporal.Queries; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; import java.time.temporal.TemporalQuery; import java.time.temporal.ValueRange; --- 65,77 ---- import static java.time.temporal.ChronoField.INSTANT_SECONDS; import java.time.DateTimeException; import java.time.Instant; import java.time.ZoneId; ! import java.time.chrono.Chronology; import java.time.temporal.ChronoField; ! import java.time.chrono.ChronoLocalDate; import java.time.temporal.Queries; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; import java.time.temporal.TemporalQuery; import java.time.temporal.ValueRange;
*** 79,94 **** import java.util.Objects; /** * Context object used during date and time printing. * <p> ! * This class provides a single wrapper to items used in the print. * * <h3>Specification for implementors</h3> * This class is a mutable context intended for use from a single thread. * Usage of the class is thread-safe within standard printing as the framework creates ! * a new instance of the class for each print and printing is single-threaded. * * @since 1.8 */ final class DateTimePrintContext { --- 79,94 ---- import java.util.Objects; /** * Context object used during date and time printing. * <p> ! * This class provides a single wrapper to items used in the format. * * <h3>Specification for implementors</h3> * This class is a mutable context intended for use from a single thread. * Usage of the class is thread-safe within standard printing as the framework creates ! * a new instance of the class for each format and printing is single-threaded. * * @since 1.8 */ final class DateTimePrintContext {
*** 107,134 **** /** * Creates a new instance of the context. * * @param temporal the temporal object being output, not null ! * @param formatter the formatter controlling the print, not null */ DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) { super(); this.temporal = adjust(temporal, formatter); this.formatter = formatter; } private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) { // normal case first ! Chrono<?> overrideChrono = formatter.getChrono(); ZoneId overrideZone = formatter.getZone(); if (overrideChrono == null && overrideZone == null) { return temporal; } // ensure minimal change ! Chrono<?> temporalChrono = Chrono.from(temporal); // default to ISO, handles Instant ZoneId temporalZone = temporal.query(Queries.zone()); // zone then offset, handles OffsetDateTime if (temporal.isSupported(EPOCH_DAY) == false || Objects.equals(overrideChrono, temporalChrono)) { overrideChrono = null; } if (temporal.isSupported(INSTANT_SECONDS) == false || Objects.equals(overrideZone, temporalZone)) { --- 107,134 ---- /** * Creates a new instance of the context. * * @param temporal the temporal object being output, not null ! * @param formatter the formatter controlling the format, not null */ DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) { super(); this.temporal = adjust(temporal, formatter); this.formatter = formatter; } private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) { // normal case first ! Chronology overrideChrono = formatter.getChronology(); ZoneId overrideZone = formatter.getZone(); if (overrideChrono == null && overrideZone == null) { return temporal; } // ensure minimal change ! Chronology temporalChrono = Chronology.from(temporal); // default to ISO, handles Instant ZoneId temporalZone = temporal.query(Queries.zone()); // zone then offset, handles OffsetDateTime if (temporal.isSupported(EPOCH_DAY) == false || Objects.equals(overrideChrono, temporalChrono)) { overrideChrono = null; } if (temporal.isSupported(INSTANT_SECONDS) == false || Objects.equals(overrideZone, temporalZone)) {
*** 142,153 **** if (overrideChrono != null && overrideZone != null) { return overrideChrono.zonedDateTime(Instant.from(temporal), overrideZone); } else if (overrideZone != null) { return temporalChrono.zonedDateTime(Instant.from(temporal), overrideZone); } else { // overrideChrono != null ! // need class here to handle non-standard cases like OffsetDate ! final ChronoLocalDate<?> date = overrideChrono.date(temporal); return new TemporalAccessor() { @Override public boolean isSupported(TemporalField field) { return temporal.isSupported(field); } --- 142,153 ---- if (overrideChrono != null && overrideZone != null) { return overrideChrono.zonedDateTime(Instant.from(temporal), overrideZone); } else if (overrideZone != null) { return temporalChrono.zonedDateTime(Instant.from(temporal), overrideZone); } else { // overrideChrono != null ! // need class here to handle non-standard cases ! final ChronoLocalDate date = overrideChrono.date(temporal); return new TemporalAccessor() { @Override public boolean isSupported(TemporalField field) { return temporal.isSupported(field); }
*** 158,183 **** return date.range(field); } else { return temporal.range(field); } } ! return field.doRange(this); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { if (((ChronoField) field).isDateField()) { return date.getLong(field); } else { return temporal.getLong(field); } } ! return field.doGet(this); } @Override public <R> R query(TemporalQuery<R> query) { ! if (query == Queries.zoneId() || query == Queries.chrono() || query == Queries.precision()) { return temporal.query(query); } return query.queryFrom(this); } }; --- 158,187 ---- return date.range(field); } else { return temporal.range(field); } } ! return field.rangeRefinedBy(this); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { if (((ChronoField) field).isDateField()) { return date.getLong(field); } else { return temporal.getLong(field); } } ! return field.getFrom(this); } + @SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { ! if (query == Queries.chronology()) { ! return (R) date.getChronology(); ! } ! if (query == Queries.zoneId() || query == Queries.precision()) { return temporal.query(query); } return query.queryFrom(this); } };
*** 195,205 **** } /** * Gets the locale. * <p> ! * This locale is used to control localization in the print output except * where localization is controlled by the symbols. * * @return the locale, not null */ Locale getLocale() { --- 199,209 ---- } /** * Gets the locale. * <p> ! * This locale is used to control localization in the format output except * where localization is controlled by the symbols. * * @return the locale, not null */ Locale getLocale() {