Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# HG changeset patch
# User scolebourne
# Date 1394626494 0
# Node ID 7b1e9663ed48d903ee6ce1b21e9e9bab8ea17623
# Parent e66f488e6f7fa6a3be8d1254d0c52bda346b9574
JDK-8033662 DateTimeFormatter doesn't set zone when parsing
diff --git a/src/share/classes/java/time/format/DateTimeParseContext.java b/src/share/classes/java/time/format/DateTimeParseContext.java
--- a/src/share/classes/java/time/format/DateTimeParseContext.java
+++ b/src/share/classes/java/time/format/DateTimeParseContext.java
@@ -315,7 +315,9 @@
*/
Parsed toParsed() {
Parsed parsed = currentParsed();
+ // using 'effective' fields in Parsed delay visibility of values until resolve phase starts
parsed.effectiveChrono = getEffectiveChronology();
+ parsed.effectiveZone = (parsed.zone != null ? parsed.zone : formatter.getZone());
return parsed;
}
diff --git a/src/share/classes/java/time/format/Parsed.java b/src/share/classes/java/time/format/Parsed.java
--- a/src/share/classes/java/time/format/Parsed.java
+++ b/src/share/classes/java/time/format/Parsed.java
@@ -140,6 +140,10 @@
*/
Chronology effectiveChrono;
/**
+ * The effective zone.
+ */
+ ZoneId effectiveZone;
+ /**
* The resolver style to use.
*/
private ResolverStyle resolverStyle;
@@ -242,6 +246,7 @@
}
this.resolverStyle = resolverStyle;
chrono = effectiveChrono;
+ zone = effectiveZone;
resolveFields();
resolveTimeLenient();
crossCheck();
@@ -266,14 +271,16 @@
TemporalAccessor resolvedObject = targetField.resolve(fieldValues, this, resolverStyle);
if (resolvedObject != null) {
if (resolvedObject instanceof ChronoZonedDateTime) {
- ChronoZonedDateTime<?> czdt = (ChronoZonedDateTime) resolvedObject;
- if (zone.equals(czdt.getZone()) == false) {
+ ChronoZonedDateTime<?> czdt = (ChronoZonedDateTime<?>) resolvedObject;
+ if (zone == null) {
+ zone = czdt.getZone();
+ } else if (zone.equals(czdt.getZone()) == false) {
throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " + zone);
}
resolvedObject = czdt.toLocalDateTime();
}
if (resolvedObject instanceof ChronoLocalDateTime) {
- ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime) resolvedObject;
+ ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime<?>) resolvedObject;
updateCheckConflict(cldt.toLocalTime(), Period.ZERO);
updateCheckConflict(cldt.toLocalDate());
changedCount++;
diff --git a/test/java/time/tck/java/time/format/TCKDateTimeParseResolver.java b/test/java/time/tck/java/time/format/TCKDateTimeParseResolver.java
--- a/test/java/time/tck/java/time/format/TCKDateTimeParseResolver.java
+++ b/test/java/time/tck/java/time/format/TCKDateTimeParseResolver.java
@@ -90,9 +90,6 @@
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.YEAR;
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
-import static java.time.temporal.ChronoUnit.DAYS;
-import static java.time.temporal.ChronoUnit.FOREVER;
-import static java.time.temporal.ChronoUnit.NANOS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
@@ -102,13 +99,17 @@
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
-import java.time.chrono.Chronology;
+import java.time.chrono.ChronoLocalDate;
+import java.time.chrono.ChronoLocalDateTime;
+import java.time.chrono.ChronoZonedDateTime;
+import java.time.chrono.IsoChronology;
+import java.time.chrono.MinguoChronology;
+import java.time.chrono.MinguoDate;
import java.time.chrono.ThaiBuddhistChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
-import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
@@ -129,6 +130,9 @@
// TODO: tests with weird TenporalField implementations
// TODO: tests with non-ISO chronologies
+ private static final ZoneId EUROPE_ATHENS = ZoneId.of("Europe/Athens");
+ private static final ZoneId EUROPE_PARIS = ZoneId.of("Europe/Paris");
+
//-----------------------------------------------------------------------
@DataProvider(name="resolveOneNoChange")
Object[][] data_resolveOneNoChange() {
@@ -886,205 +890,273 @@
}
//-----------------------------------------------------------------------
+ // SPEC: DateTimeFormatter.withChronology()
@Test
- public void test_fieldResolvesToLocalTime() {
- TemporalField field = new TemporalField() {
- @Override
- public TemporalUnit getBaseUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalUnit getRangeUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange range() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isDateBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isTimeBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isSupportedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public long getFrom(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public <R extends Temporal> R adjustInto(R temporal, long newValue) {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalAccessor resolve(
- Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
- return LocalTime.MIDNIGHT.plusNanos(fieldValues.remove(this));
- }
- };
- DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field).toFormatter();
- TemporalAccessor accessor = f.parse("1234567890");
- assertEquals(accessor.query(TemporalQueries.localDate()), null);
- assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.of(0, 0, 1, 234_567_890));
+ public void test_withChronology_noOverride() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
+ TemporalAccessor accessor = f.parse("");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
}
@Test
- public void test_fieldResolvesToChronoLocalDateTime() {
- TemporalField field = new TemporalField() {
- @Override
- public TemporalUnit getBaseUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalUnit getRangeUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange range() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isDateBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isTimeBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isSupportedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public long getFrom(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public <R extends Temporal> R adjustInto(R temporal, long newValue) {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalAccessor resolve(
- Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
- fieldValues.remove(this);
- return LocalDateTime.of(2010, 6, 30, 12, 30);
- }
- };
- DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field).toFormatter();
+ public void test_withChronology_override() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ TemporalAccessor accessor = f.parse("");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
+ }
+
+ @Test
+ public void test_withChronology_parsedChronology_noOverride() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendChronologyId().toFormatter();
+ TemporalAccessor accessor = f.parse("ThaiBuddhist");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.chronology()), ThaiBuddhistChronology.INSTANCE);
+ }
+
+ @Test
+ public void test_withChronology_parsedChronology_override() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendChronologyId().toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ TemporalAccessor accessor = f.parse("ThaiBuddhist");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.chronology()), ThaiBuddhistChronology.INSTANCE);
+ }
+
+ //-----------------------------------------------------------------------
+ // SPEC: DateTimeFormatter.withZone()
+ @Test
+ public void test_withZone_noOverride() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
+ TemporalAccessor accessor = f.parse("");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.zoneId()), null);
+ }
+
+ @Test
+ public void test_withZone_override() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).toFormatter();
+ f = f.withZone(EUROPE_ATHENS);
+ TemporalAccessor accessor = f.parse("");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_ATHENS);
+ }
+
+ @Test
+ public void test_withZone_parsedZone_noOverride() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendZoneId().toFormatter();
+ TemporalAccessor accessor = f.parse("Europe/Paris");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
+ }
+
+ @Test
+ public void test_withZone_parsedZone_override() {
+ DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendZoneId().toFormatter();
+ f = f.withZone(EUROPE_ATHENS);
+ TemporalAccessor accessor = f.parse("Europe/Paris");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
+ }
+
+ //-----------------------------------------------------------------------
+ @Test
+ public void test_fieldResolvesToLocalTime() {
+ LocalTime lt = LocalTime.of(12, 30, 40);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(lt)).toFormatter();
+ TemporalAccessor accessor = f.parse("1234567890");
+ assertEquals(accessor.query(TemporalQueries.localDate()), null);
+ assertEquals(accessor.query(TemporalQueries.localTime()), lt);
+ }
+
+ //-------------------------------------------------------------------------
+ @Test
+ public void test_fieldResolvesToChronoLocalDate_noOverrideChrono_matches() {
+ LocalDate ldt = LocalDate.of(2010, 6, 30);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(ldt)).toFormatter();
TemporalAccessor accessor = f.parse("1234567890");
assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(2010, 6, 30));
- assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.of(12, 30));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
+ }
+
+ @Test
+ public void test_fieldResolvesToChronoLocalDate_overrideChrono_matches() {
+ MinguoDate mdt = MinguoDate.of(100, 6, 30);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mdt)).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ TemporalAccessor accessor = f.parse("1234567890");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
+ assertEquals(accessor.query(TemporalQueries.localTime()), null);
+ assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
}
@Test(expectedExceptions = DateTimeParseException.class)
- public void test_fieldResolvesWrongChrono() {
- TemporalField field = new TemporalField() {
- @Override
- public TemporalUnit getBaseUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalUnit getRangeUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange range() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isDateBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isTimeBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isSupportedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public long getFrom(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public <R extends Temporal> R adjustInto(R temporal, long newValue) {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalAccessor resolve(
- Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
- return ThaiBuddhistChronology.INSTANCE.dateNow();
- }
- };
- DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field).toFormatter();
+ public void test_fieldResolvesToChronoLocalDate_noOverrideChrono_wrongChrono() {
+ ChronoLocalDate cld = ThaiBuddhistChronology.INSTANCE.dateNow();
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cld)).toFormatter();
f.parse("1234567890");
}
@Test(expectedExceptions = DateTimeParseException.class)
- public void test_fieldResolvesWrongZone() {
- TemporalField field = new TemporalField() {
- @Override
- public TemporalUnit getBaseUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalUnit getRangeUnit() {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange range() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isDateBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isTimeBased() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isSupportedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public long getFrom(TemporalAccessor temporal) {
- throw new UnsupportedOperationException();
- }
- @Override
- public <R extends Temporal> R adjustInto(R temporal, long newValue) {
- throw new UnsupportedOperationException();
- }
- @Override
- public TemporalAccessor resolve(
- Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
- return ZonedDateTime.of(2010, 6, 30, 12, 30, 0, 0, ZoneId.of("Europe/Paris"));
- }
- };
- DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field).toFormatter().withZone(ZoneId.of("Europe/London"));
+ public void test_fieldResolvesToChronoLocalDate_overrideChrono_wrongChrono() {
+ ChronoLocalDate cld = ThaiBuddhistChronology.INSTANCE.dateNow();
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cld)).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
f.parse("1234567890");
}
+ //-------------------------------------------------------------------------
+ @Test
+ public void test_fieldResolvesToChronoLocalDateTime_noOverrideChrono_matches() {
+ LocalDateTime ldt = LocalDateTime.of(2010, 6, 30, 12, 30);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(ldt)).toFormatter();
+ TemporalAccessor accessor = f.parse("1234567890");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(2010, 6, 30));
+ assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.of(12, 30));
+ assertEquals(accessor.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
+ }
+
+ @Test
+ public void test_fieldResolvesToChronoLocalDateTime_overrideChrono_matches() {
+ MinguoDate mdt = MinguoDate.of(100, 6, 30);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mdt.atTime(LocalTime.NOON))).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ TemporalAccessor accessor = f.parse("1234567890");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
+ assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
+ assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
+ }
+
+ @Test(expectedExceptions = DateTimeParseException.class)
+ public void test_fieldResolvesToChronoLocalDateTime_noOverrideChrono_wrongChrono() {
+ ChronoLocalDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
+ f.parse("1234567890");
+ }
+
+ @Test(expectedExceptions = DateTimeParseException.class)
+ public void test_fieldResolvesToChronoLocalDateTime_overrideChrono_wrongChrono() {
+ ChronoLocalDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ f.parse("1234567890");
+ }
+
+ //-------------------------------------------------------------------------
+ @Test
+ public void test_fieldResolvesToChronoZonedDateTime_noOverrideChrono_matches() {
+ ZonedDateTime zdt = ZonedDateTime.of(2010, 6, 30, 12, 30, 0, 0, EUROPE_PARIS);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(zdt)).toFormatter();
+ TemporalAccessor accessor = f.parse("1234567890");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(2010, 6, 30));
+ assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.of(12, 30));
+ assertEquals(accessor.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
+ assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
+ }
+
+ @Test
+ public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() {
+ MinguoDate mdt = MinguoDate.of(100, 6, 30);
+ ChronoZonedDateTime<MinguoDate> mzdt = mdt.atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mzdt)).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ TemporalAccessor accessor = f.parse("1234567890");
+ assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
+ assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
+ assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
+ assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
+ }
+
+ @Test(expectedExceptions = DateTimeParseException.class)
+ public void test_fieldResolvesToChronoZonedDateTime_noOverrideChrono_wrongChrono() {
+ ChronoZonedDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
+ f.parse("1234567890");
+ }
+
+ @Test(expectedExceptions = DateTimeParseException.class)
+ public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_wrongChrono() {
+ ChronoZonedDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
+ f = f.withChronology(MinguoChronology.INSTANCE);
+ f.parse("1234567890");
+ }
+
+ @Test
+ public void test_fieldResolvesToChronoZonedDateTime_overrideZone_matches() {
+ ZonedDateTime zdt = ZonedDateTime.of(2010, 6, 30, 12, 30, 0, 0, EUROPE_PARIS);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(zdt)).toFormatter();
+ f = f.withZone(EUROPE_PARIS);
+ assertEquals(f.parse("1234567890", ZonedDateTime::from), zdt);
+ }
+
+ @Test(expectedExceptions = DateTimeParseException.class)
+ public void test_fieldResolvesToChronoZonedDateTime_overrideZone_wrongZone() {
+ ZonedDateTime zdt = ZonedDateTime.of(2010, 6, 30, 12, 30, 0, 0, EUROPE_PARIS);
+ DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(zdt)).toFormatter();
+ f = f.withZone(ZoneId.of("Europe/London"));
+ f.parse("1234567890");
+ }
+
+ //-------------------------------------------------------------------------
+ private static class ResolvingField implements TemporalField {
+ private final TemporalAccessor resolvedValue;
+ ResolvingField(TemporalAccessor resolvedValue) {
+ this.resolvedValue = resolvedValue;
+ }
+ @Override
+ public TemporalUnit getBaseUnit() {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public TemporalUnit getRangeUnit() {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public ValueRange range() {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public boolean isDateBased() {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public boolean isTimeBased() {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public boolean isSupportedBy(TemporalAccessor temporal) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public long getFrom(TemporalAccessor temporal) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public <R extends Temporal> R adjustInto(R temporal, long newValue) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public TemporalAccessor resolve(
+ Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
+ fieldValues.remove(this);
+ return resolvedValue;
+ }
+ };
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment