@Test(dataProvider="resolveFourToTime") public void test_resolveFourToTime(ResolverStyle style, long hour, long min, long sec, long nano, LocalTime expectedTime, Period excessPeriod) { DateTimeFormatter f = new DateTimeFormatterBuilder() .parseDefaulting(HOUR_OF_DAY, hour) .parseDefaulting(MINUTE_OF_HOUR, min) .parseDefaulting(SECOND_OF_MINUTE, sec) .parseDefaulting(NANO_OF_SECOND, nano).toFormatter(); ResolverStyle[] styles = (style != null ? new ResolverStyle[] {style} : ResolverStyle.values()); for (ResolverStyle s : styles) { if (expectedTime != null) { TemporalAccessor accessor = f.withResolverStyle(s).parse(""); assertEquals(accessor.query(TemporalQueries.localDate()), null, "ResolverStyle: " + s); assertEquals(accessor.query(TemporalQueries.localTime()), expectedTime, "ResolverStyle: " + s); assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), excessPeriod, "ResolverStyle: " + s); } else { try { f.withResolverStyle(style).parse(""); fail(); } catch (DateTimeParseException ex) { // expected } } } }
@Test(dataProvider="parseSuccess") public void test_parseSuccess_caseInsensitive(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) { builder.parseCaseInsensitive().appendZoneId(); String lcText = text.toLowerCase(Locale.ENGLISH); TemporalAccessor parsed = builder.toFormatter().parseUnresolved(lcText, pos); assertEquals(pos.getErrorIndex(), expectedErrorIndex, "Incorrect error index parsing: " + lcText); assertEquals(pos.getIndex(), expectedIndex, "Incorrect index parsing: " + lcText); if (expected != null) { ZoneId zid = parsed.query(TemporalQueries.zoneId()); assertEquals(parsed.query(TemporalQueries.zoneId()), expected, "Incorrect zoneId parsing: " + lcText); assertEquals(parsed.query(TemporalQueries.offset()), null, "Incorrect offset parsing: " + lcText); assertEquals(parsed.query(TemporalQueries.zone()), expected, "Incorrect zone parsing: " + lcText); } else { assertEquals(parsed, null); } }
@Test(dataProvider="sample_isoZonedDateTime") public void test_print_isoZonedDateTime( Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(year, month, day, hour, min, sec, nano, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(test), expected); } else { try { DateTimeFormatter.ISO_ZONED_DATE_TIME.format(test); fail(test.toString()); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test(dataProvider="LocalWeekBasedYearPatterns") public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { WeekFields weekDef = WeekFields.of(locale); assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0); assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0); assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef); } }
@Override public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { Objects.requireNonNull(obj, "obj"); Objects.requireNonNull(toAppendTo, "toAppendTo"); Objects.requireNonNull(pos, "pos"); if (obj instanceof TemporalAccessor == false) { throw new IllegalArgumentException("Format target must implement TemporalAccessor"); } pos.setBeginIndex(0); pos.setEndIndex(0); try { formatter.formatTo((TemporalAccessor) obj, toAppendTo); } catch (RuntimeException ex) { throw new IllegalArgumentException(ex.getMessage(), ex); } return toAppendTo; }
@Test(dataProvider="parseSuccess") public void test_parseSuccess_prefix(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) { builder.appendZoneId(); pos.setIndex(3); String prefixText = "XXX" + text; TemporalAccessor parsed = builder.toFormatter().parseUnresolved(prefixText, pos); assertEquals(pos.getErrorIndex(), expectedErrorIndex >= 0 ? expectedErrorIndex + 3 : expectedErrorIndex, "Incorrect error index parsing: " + prefixText); assertEquals(pos.getIndex(), expectedIndex + 3, "Incorrect index parsing: " + prefixText); if (expected != null) { assertEquals(parsed.query(TemporalQueries.zoneId()), expected, "Incorrect zoneId parsing: " + prefixText); assertEquals(parsed.query(TemporalQueries.offset()), null, "Incorrect offset parsing: " + prefixText); assertEquals(parsed.query(TemporalQueries.zone()), expected, "Incorrect zone parsing: " + prefixText); } else { assertEquals(parsed, null); } }
/** * Parses and resolves the specified text. * <p> * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed. * * @param text the text to parse, not null * @param position the position to parse from, updated with length parsed * and the index of any error, null if parsing whole string * @return the resolved result of the parse, not null * @throws DateTimeParseException if the parse fails * @throws DateTimeException if an error occurs while resolving the date or time * @throws IndexOutOfBoundsException if the position is invalid */ private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) { ParsePosition pos = (position != null ? position : new ParsePosition(0)); DateTimeParseContext context = parseUnresolved0(text, pos); if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) { String abbr; if (text.length() > 64) { abbr = text.subSequence(0, 64).toString() + "..."; } else { abbr = text.toString(); } if (pos.getErrorIndex() >= 0) { throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " + pos.getErrorIndex(), text, pos.getErrorIndex()); } else { throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " + pos.getIndex(), text, pos.getIndex()); } } return context.toResolved(resolverStyle, resolverFields); }
@Test(dataProvider="sample_isoLocalTime") public void test_print_isoLocalTime( Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(null, null, null, hour, min, sec, nano, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_LOCAL_TIME.format(test), expected); } else { try { DateTimeFormatter.ISO_LOCAL_TIME.format(test); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test(dataProvider="LocalWeekMonthYearPatterns") public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(YEAR_OF_ERA), true); assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true); assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) || parsed.isSupported(WeekFields.of(locale).weekOfYear()), true); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern); } }
@Test(dataProvider="sample_isoOffsetDateTime") public void test_print_isoOffsetDateTime( Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(year, month, day, hour, min, sec, nano, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(test), expected); } else { try { DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(test); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test() public void basicTest_get_TemporalField_supported() { for (TemporalAccessor sample : samples()) { for (TemporalField field : validFields()) { if (sample.range(field).isIntValue()) { sample.get(field); // no exception } else { try { sample.get(field); fail("Failed on " + sample + " " + field); } catch (DateTimeException ex) { // expected } } } } }
@Test(dataProvider="sample_isoDateTime") public void test_print_isoDateTime( Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(year, month, day, hour, min, sec, nano, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_DATE_TIME.format(test), expected); } else { try { DateTimeFormatter.ISO_DATE_TIME.format(test); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test(dataProvider="resolveOneToField") public void test_resolveOneToField(TemporalField field1, long value1, TemporalField expectedField1, Long expectedValue1, TemporalField expectedField2, Long expectedValue2) { String str = Long.toString(value1); DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter(); TemporalAccessor accessor = f.parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), null); assertEquals(accessor.query(TemporalQueries.localTime()), null); if (expectedField1 != null) { assertEquals(accessor.isSupported(expectedField1), true); assertEquals(accessor.getLong(expectedField1), expectedValue1.longValue()); } if (expectedField2 != null) { assertEquals(accessor.isSupported(expectedField2), true); assertEquals(accessor.getLong(expectedField2), expectedValue2.longValue()); } }
@Test(dataProvider="sample_isoOffsetTime") public void test_print_isoOffsetTime( Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(null, null, null, hour, min, sec, nano, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_OFFSET_TIME.format(test), expected); } else { try { DateTimeFormatter.ISO_OFFSET_TIME.format(test); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test(dataProvider="sample_isoOffsetDate") public void test_print_isoOffsetDate( Integer year, Integer month, Integer day, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(year, month, day, null, null, null, null, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_OFFSET_DATE.format(test), expected); } else { try { DateTimeFormatter.ISO_OFFSET_DATE.format(test); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test public void test_reducedWithLateChronoChangeTwice() { DateTimeFormatter df = new DateTimeFormatterBuilder() .appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1)) .appendLiteral(" ") .appendChronologyId() .appendLiteral(" ") .appendChronologyId() .toFormatter(); int expected = 2044; String input = "44 ThaiBuddhist ISO"; ParsePosition pos = new ParsePosition(0); TemporalAccessor parsed = df.parseUnresolved(input, pos); assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos); assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)"); int actual = parsed.get(YEAR); assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", parsed.query(TemporalQueries.chronology()), input)); }
@Test(dataProvider="sample_isoTime") public void test_print_isoTime( Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String expected, Class<?> expectedEx) { TemporalAccessor test = buildAccessor(null, null, null, hour, min, sec, nano, offsetId, zoneId); if (expectedEx == null) { assertEquals(DateTimeFormatter.ISO_TIME.format(test), expected); } else { try { DateTimeFormatter.ISO_TIME.format(test); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@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 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_parse_decoratedEmpty_strict() { builder.padNext(4, '-').optionalStart().appendValue(DAY_OF_MONTH).optionalEnd(); TemporalAccessor parsed = builder.toFormatter().parseUnresolved("----", pos); assertEquals(pos.getIndex(), 4); assertEquals(pos.getErrorIndex(), -1); assertNotNull(parsed); }
private <T> T getUniqueValue(final long counter, final Class<T> type) { if (Number.class.isAssignableFrom(type)) { return NumberProvider.convertNumber(new BigDecimal(counter), type); } if (java.util.Date.class.isAssignableFrom(type) || TemporalAccessor.class.isAssignableFrom(type)) { return AbstractDateProvider.convertDate(new java.util.Date(counter), type); } if (boolean.class.equals(type) || Boolean.class.equals(type)) { if (counter > 1) throw new NoDefaultValueException("Can only generate 2 unique boolean values. If this boolean is part of a bigger unique index, you cannot use the DefaultDefaultValueService anymore."); return (T) new Boolean(counter == 1); } if (char.class.equals(type) || Character.class.equals(type)) { if (counter >= 0xffff) throw new NoDefaultValueException("Can only generate 65,535 unique char values. If this char is part of a bigger unique index, you cannot use the DefaultDefaultValueService anymore."); return (T) new Character((char) (counter + 1)); } if (String.class.isAssignableFrom(type)) { StringBuilder result = new StringBuilder(); long remainder = counter; int maxExponent = -1; for (int i = 1; i < 14; i++) { if (Math.pow(26, i) > remainder) { maxExponent = i - 1; break; } } for (int i = maxExponent; i >= 0; i--) { long denominator = (long) Math.pow(26, i); int num = (int) (remainder / denominator); if (i != 0) num--; remainder = remainder % denominator; result.append((char) (num + 'A')); } return (T) result.toString(); } return null; }
@Test(dataProvider="parseDigitsLenient") public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { setStrict(false); ParsePosition pos = new ParsePosition(0); TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal); assertEquals(parsed.query(TemporalQueries.chronology()), null); assertEquals(parsed.query(TemporalQueries.zoneId()), null); } }
@Test(dataProvider="ParseLenientSensitive") public void test_parseLenient(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos, Pair strict, Pair lenient) { ParsePosition ppos = new ParsePosition(pos); setStrict(false); TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), lenient.parseLen, "error case parse position"); assertEquals(parsed, lenient.parseVal, "unexpected parse result"); } else { assertEquals(ppos.getIndex(), lenient.parseLen, "parse position"); assertParsed(parsed, YEAR, lenient.parseVal != null ? (long) lenient.parseVal : null); } }
@Test public void test_appendValue_subsequent3_parse6() throws Exception { builder .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendValue(MONTH_OF_YEAR, 2) .appendValue(DAY_OF_MONTH, 2); DateTimeFormatter f = builder.toFormatter(); assertEquals(f.toString(), "Value(Year,4,10,EXCEEDS_PAD)Value(MonthOfYear,2)Value(DayOfMonth,2)"); TemporalAccessor parsed = f.parseUnresolved("20090630", new ParsePosition(0)); assertEquals(parsed.getLong(YEAR), 2009L); assertEquals(parsed.getLong(MONTH_OF_YEAR), 6L); assertEquals(parsed.getLong(DAY_OF_MONTH), 30L); }
private void assertParseMatch(TemporalAccessor parsed, Expected expected) { for (TemporalField field : expected.fieldValues.keySet()) { assertEquals(parsed.isSupported(field), true); parsed.getLong(field); } assertEquals(parsed.query(TemporalQueries.chronology()), expected.chrono); assertEquals(parsed.query(TemporalQueries.zoneId()), expected.zone); }
@Test public void test_adjacent_lenient_firstVariableWidth_success() throws Exception { // succeeds greedily parsing variable width, then fixed width, to non-numeric Z DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK); ParsePosition pp = new ParsePosition(0); TemporalAccessor parsed = f.parseUnresolved("12309Z", pp); assertEquals(pp.getErrorIndex(), -1); assertEquals(pp.getIndex(), 6); assertEquals(parsed.getLong(HOUR_OF_DAY), 123L); assertEquals(parsed.getLong(MINUTE_OF_HOUR), 9L); }
/** * Validates that the temporal has the correct chronology. */ private void validateChrono(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && IsoChronology.INSTANCE.equals(temporalChrono) == false) { throw new DateTimeException("Chronology mismatch, expected: ISO, actual: " + temporalChrono.getId()); } }
@Test(dataProvider="parseValid") public void test_parseValid_caseSensitive(String text, Chronology expected) { builder.appendChronologyId(); TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text, pos); assertEquals(pos.getIndex(), expected.getId().length()); assertEquals(pos.getErrorIndex(), -1); assertEquals(parsed.query(TemporalQueries.chronology()), expected); }
@Override public HijrahDate date(TemporalAccessor temporal) { if (temporal instanceof HijrahDate) { return (HijrahDate) temporal; } return HijrahDate.ofEpochDay(this, temporal.getLong(EPOCH_DAY)); }
@Test 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_resolverFields_listOfOneMatching() throws Exception { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).toFormatter().withResolverFields(YEAR); TemporalAccessor parsed = f.parse("2012"); assertEquals(parsed.isSupported(YEAR), true); }
private TemporalAccessor buildAccessorInstant(long instantSecs, Integer nano) { MockAccessor mock = new MockAccessor(); mock.fields.put(INSTANT_SECONDS, instantSecs); if (nano != null) { mock.fields.put(NANO_OF_SECOND, (long) nano); } return mock; }
@Test(dataProvider="parseLenient") public void test_parseLenient(String text, int expectedIndex, int expectedErrorIndex, Number expectedMonth) { builder.parseLenient().padNext(3, '#').appendValue(MONTH_OF_YEAR, 1, 3, SignStyle.NORMAL); TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text, pos); assertEquals(pos.getIndex(), expectedIndex); assertEquals(pos.getErrorIndex(), expectedErrorIndex); if (expectedMonth != null) { assertEquals(parsed.isSupported(MONTH_OF_YEAR), true); assertEquals(parsed.getLong(MONTH_OF_YEAR), expectedMonth.longValue()); } else { assertEquals(parsed, null); } }
@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(dataProvider="resolveTwoToDate") public void test_resolveTwoToDate(TemporalField field1, long value1, TemporalField field2, long value2, LocalDate expectedDate) { String str = value1 + " " + value2; DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(field1).appendLiteral(' ') .appendValue(field2).toFormatter(); TemporalAccessor accessor = f.parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate); assertEquals(accessor.query(TemporalQueries.localTime()), null); }
@Test() public void basicTest_get_TemporalField_unsupported() { for (TemporalAccessor sample : samples()) { for (TemporalField field : invalidFields()) { try { sample.get(field); fail("Failed on " + sample + " " + field); } catch (DateTimeException ex) { // expected } } } }
private TemporalAccessor buildAccessor(LocalDate base, String offsetId, String zoneId) { MockAccessor mock = new MockAccessor(); mock.setFields(base); mock.setOffset(offsetId); mock.setZone(zoneId); return mock; }
public void test_parse_caseSensitiveUTC_matchedCase() throws Exception { setCaseSensitive(true); ParsePosition pos = new ParsePosition(0); TemporalAccessor parsed = getFormatter("+HH:MM:ss", "Z").parseUnresolved("Z", pos); assertEquals(pos.getIndex(), 1); assertParsed(parsed, ZoneOffset.UTC); }
@Test public void test_adjacent_strict_firstVariableWidth_success() throws Exception { // succeeds greedily parsing variable width, then fixed width, to non-numeric Z DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK); ParsePosition pp = new ParsePosition(0); TemporalAccessor parsed = f.parseUnresolved("12309Z", pp); assertEquals(pp.getErrorIndex(), -1); assertEquals(pp.getIndex(), 6); assertEquals(parsed.getLong(HOUR_OF_DAY), 123L); assertEquals(parsed.getLong(MINUTE_OF_HOUR), 9L); }