@Test(dataProvider="resolveFourToDate") public void test_resolveFourToDate(TemporalField field1, long value1, TemporalField field2, long value2, TemporalField field3, long value3, TemporalField field4, long value4, LocalDate expectedDate) { String str = value1 + " " + value2 + " " + value3 + " " + value4; DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(field1).appendLiteral(' ') .appendValue(field2).appendLiteral(' ') .appendValue(field3).appendLiteral(' ') .appendValue(field4).toFormatter(); TemporalAccessor accessor = f.parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate); assertEquals(accessor.query(TemporalQueries.localTime()), null); }
public void testCalculateDateTimeDateTime() { WeightedDateSimilarity sim = new WeightedDateSimilarity(0, 0, 1); sim.setYearRange(10); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(Locale.ENGLISH); LocalDateTime dt1 = LocalDateTime.parse("2015-01-01", formatter); LocalDateTime dt2 = LocalDateTime.parse("2014-01-01", formatter); LocalDateTime dt3 = LocalDateTime.parse("2010-01-01", formatter); LocalDateTime dt4 = LocalDateTime.parse("2005-01-01", formatter); LocalDateTime dt5 = LocalDateTime.parse("1905-01-01", formatter); assertEquals(1.0, sim.calculate(dt1, dt1)); assertEquals(0.9, sim.calculate(dt1, dt2)); assertEquals(0.9, sim.calculate(dt2, dt1)); assertEquals(0.5, sim.calculate(dt1, dt3)); assertEquals(0.0, sim.calculate(dt1, dt4)); assertEquals(0.0, sim.calculate(dt1, dt5)); }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) { LocalDate date = LocalDate.of(2012, 12, 15); WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField womField = week.weekOfMonth(); for (int i = 1; i <= 60; i++) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(MONTH_OF_YEAR).appendLiteral(':') .appendValue(womField).appendLiteral(':') .appendValue(dowField).toFormatter().withResolverStyle(LENIENT); int wom = date.get(womField); int dow = date.get(dowField); for (int j = wom - 10; j < wom + 10; j++) { String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow; LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j); } date = date.plusDays(1); } }
private DataSet<Movie, Attribute> generateDS2() { DataSet<Movie, Attribute> ds = new HashedDataSet<>(); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(Locale.ENGLISH); Movie m1 = new Movie("4", "DS2"); m1.setDate(LocalDateTime.parse("1983-10-10", formatter)); ds.add(m1); Movie m2 = new Movie("5", "DS2"); m2.setDate(LocalDateTime.parse("1984-10-10", formatter)); ds.add(m2); Movie m3 = new Movie("6", "DS2"); m3.setDate(LocalDateTime.parse("1995-10-10", formatter)); ds.add(m3); return ds; }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWomDow(DayOfWeek firstDayOfWeek, int minDays) { LocalDate date = LocalDate.of(2012, 12, 15); WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField womField = week.weekOfMonth(); for (int i = 1; i <= 15; i++) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(MONTH_OF_YEAR).appendLiteral(':') .appendValue(womField).appendLiteral(':') .appendValue(dowField).toFormatter(); String str = date.getYear() + ":" + date.getMonthValue() + ":" + date.get(womField) + ":" + date.get(dowField); LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, date, " :: " + str + " " + i); date = date.plusDays(1); } }
@Test(dataProvider = "parseLenientQuarter") public void test_parse_parseLenientQuarter_SMART(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.SMART); if (smart) { LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, expected); } else { try { LocalDate.parse(str, f); fail("Should have failed"); } catch (DateTimeParseException ex) { // expected } } }
@Test(dataProvider = "signStyle") public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) { DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style) .toFormatter(); formatter = formatter.withZone(ZoneOffset.UTC); if (expectedEx == null) { String output = formatter.format(localDate); assertEquals(output, expectedStr); } else { try { formatter.format(localDate); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test(dataProvider="resolveClockHourOfDay") public void test_resolveClockHourOfDay(ResolverStyle style, long value, Integer expectedHour, int expectedDays) { String str = Long.toString(value); DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(CLOCK_HOUR_OF_DAY).toFormatter(); if (expectedHour != null) { TemporalAccessor accessor = f.withResolverStyle(style).parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), null); assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.of(expectedHour, 0)); assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ofDays(expectedDays)); } else { try { f.withResolverStyle(style).parse(str); fail(); } catch (DateTimeParseException ex) { // expected } } }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWom_lenient(DayOfWeek firstDayOfWeek, int minDays) { LocalDate date = LocalDate.of(2012, 12, 15); WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField womField = week.weekOfMonth(); for (int i = 1; i <= 60; i++) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(MONTH_OF_YEAR).appendLiteral(':') .appendValue(womField).appendLiteral(':') .appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT); int wom = date.get(womField); int dow = date.get(DAY_OF_WEEK); for (int j = wom - 10; j < wom + 10; j++) { String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow; LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j); } date = date.plusDays(1); } }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWoy(DayOfWeek firstDayOfWeek, int minDays) { LocalDate date = LocalDate.of(2012, 12, 15); WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 60; i++) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(woyField).appendLiteral(':') .appendValue(DAY_OF_WEEK).toFormatter(); String str = date.getYear() + ":" + date.get(woyField) + ":" + date.get(DAY_OF_WEEK); LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, date, " :: " + str + " " + i); date = date.plusDays(1); } }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWoyDow_lenient(DayOfWeek firstDayOfWeek, int minDays) { LocalDate date = LocalDate.of(2012, 12, 15); WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 60; i++) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(woyField).appendLiteral(':') .appendValue(dowField).toFormatter().withResolverStyle(LENIENT); int woy = date.get(woyField); int dow = date.get(dowField); for (int j = woy - 60; j < woy + 60; j++) { String str = date.getYear() + ":" + j + ":" + dow; LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, date.plusWeeks(j - woy), " ::" + str + ": :" + i + "::" + j); } date = date.plusDays(1); } }
@Test(dataProvider="ReducedWithChrono") public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) { Chronology chrono = date.getChronology(); DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1)) .toFormatter() .withChronology(chrono); int expected = date.get(YEAR_OF_ERA); String input = df.format(date); ParsePosition pos = new ParsePosition(0); TemporalAccessor parsed = df.parseUnresolved(input, pos); int actual = parsed.get(YEAR_OF_ERA); assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", chrono, input)); }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWoWBYDow(DayOfWeek firstDayOfWeek, int minDays) { LocalDate date = LocalDate.of(2012, 12, 31); WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField wowbyField = week.weekOfWeekBasedYear(); TemporalField yowbyField = week.weekBasedYear(); for (int i = 1; i <= 60; i++) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(yowbyField).appendLiteral(':') .appendValue(wowbyField).appendLiteral(':') .appendValue(dowField).toFormatter(); String str = date.get(yowbyField) + ":" + date.get(wowbyField) + ":" + date.get(dowField); LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, date, " :: " + str + " " + i); date = date.plusDays(1); } }
@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); } }
@Test(dataProvider="resolveThreeNoChange") public void test_resolveThreeNoChange(TemporalField field1, long value1, TemporalField field2, long value2, TemporalField field3, long value3) { String str = value1 + " " + value2 + " " + value3; DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(field1).appendLiteral(' ') .appendValue(field2).appendLiteral(' ') .appendValue(field3).toFormatter(); TemporalAccessor accessor = f.parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), null); assertEquals(accessor.query(TemporalQueries.localTime()), null); assertEquals(accessor.isSupported(field1), true); assertEquals(accessor.isSupported(field2), true); assertEquals(accessor.isSupported(field3), true); assertEquals(accessor.getLong(field1), value1); assertEquals(accessor.getLong(field2), value2); assertEquals(accessor.getLong(field3), value3); }
@Test(dataProvider="weekFields") public void test_parse_resolve_localizedWom_strict(DayOfWeek firstDayOfWeek, int minDays) { WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField womField = week.weekOfMonth(); DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(MONTH_OF_YEAR).appendLiteral(':') .appendValue(womField).appendLiteral(':') .appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT); String str = "2012:1:0:1"; try { LocalDate date = LocalDate.parse(str, f); assertEquals(date.getYear(), 2012); assertEquals(date.getMonthValue(), 1); assertEquals(date.get(womField), 0); assertEquals(date.get(DAY_OF_WEEK), 1); } catch (DateTimeException ex) { // expected } }
@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 public void test_resolverFields_selectOneDateResolveYD() throws Exception { DateTimeFormatter base = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).appendLiteral('-') .appendValue(DAY_OF_MONTH).appendLiteral('-').appendValue(DAY_OF_YEAR).toFormatter(); DateTimeFormatter f = base.withResolverFields(YEAR, DAY_OF_YEAR); Set<TemporalField> expected = new HashSet<>(Arrays.asList(YEAR, DAY_OF_YEAR)); // Use set.equals(); testNG comparison of Collections is ordered assertTrue(f.getResolverFields().equals(expected), "ResolveFields: " + f.getResolverFields()); try { base.parse("2012-6-30-321", LocalDate::from); // wrong month/day-of-month fail(); } catch (DateTimeException ex) { // expected, fails as it produces two different dates } LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from); // ignored month/day-of-month assertEquals(parsed, LocalDate.of(2012, 11, 16)); }
@Test(dataProvider="resolveFourToTime") public void test_resolveFourToDateTime(ResolverStyle style, long hour, long min, long sec, long nano, LocalTime expectedTime, Period excessPeriod) { DateTimeFormatter f = new DateTimeFormatterBuilder() .parseDefaulting(YEAR, 2012).parseDefaulting(MONTH_OF_YEAR, 6).parseDefaulting(DAY_OF_MONTH, 30) .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()); if (expectedTime != null && excessPeriod != null) { LocalDate expectedDate = LocalDate.of(2012, 6, 30).plus(excessPeriod); for (ResolverStyle s : styles) { TemporalAccessor accessor = f.withResolverStyle(s).parse(""); assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate, "ResolverStyle: " + s); assertEquals(accessor.query(TemporalQueries.localTime()), expectedTime, "ResolverStyle: " + s); assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ZERO, "ResolverStyle: " + s); } } }
@Test(dataProvider="resolveSecondOfDay") public void test_resolveSecondOfDay(ResolverStyle style, long value, Integer expectedSecond, int expectedDays) { String str = Long.toString(value); DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(SECOND_OF_DAY).toFormatter(); if (expectedSecond != null) { TemporalAccessor accessor = f.withResolverStyle(style).parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), null); assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.ofSecondOfDay(expectedSecond)); assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ofDays(expectedDays)); } else { try { f.withResolverStyle(style).parse(str); fail(); } catch (DateTimeParseException ex) { // expected } } }
@Test(dataProvider="resolveClockHourOfAmPm") public void test_resolveClockHourOfAmPm(ResolverStyle style, long value, Integer expectedValue) { String str = Long.toString(value); DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(CLOCK_HOUR_OF_AMPM).toFormatter(); if (expectedValue != null) { TemporalAccessor accessor = f.withResolverStyle(style).parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), null); assertEquals(accessor.query(TemporalQueries.localTime()), null); assertEquals(accessor.isSupported(CLOCK_HOUR_OF_AMPM), false); assertEquals(accessor.isSupported(HOUR_OF_AMPM), true); assertEquals(accessor.getLong(HOUR_OF_AMPM), expectedValue.longValue()); } else { try { f.withResolverStyle(style).parse(str); fail(); } catch (DateTimeParseException ex) { // expected } } }
@Test(dataProvider="resolveAmPm") public void test_resolveAmPm(ResolverStyle style, long value, Integer expectedValue) { String str = Long.toString(value); DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(AMPM_OF_DAY).toFormatter(); if (expectedValue != null) { TemporalAccessor accessor = f.withResolverStyle(style).parse(str); assertEquals(accessor.query(TemporalQueries.localDate()), null); assertEquals(accessor.query(TemporalQueries.localTime()), null); assertEquals(accessor.isSupported(AMPM_OF_DAY), true); assertEquals(accessor.getLong(AMPM_OF_DAY), expectedValue.longValue()); } else { try { f.withResolverStyle(style).parse(str); fail(); } catch (DateTimeParseException ex) { // expected } } }
@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); }
/** * Constructs a new instance of {@link OerGeneralizedTimeCodec}. */ public OerGeneralizedTimeCodec() { this.generalizedTimeFormatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4) .appendValue(ChronoField.MONTH_OF_YEAR, 2) .appendValue(ChronoField.DAY_OF_MONTH, 2) .appendValue(ChronoField.HOUR_OF_DAY, 2) .appendValue(ChronoField.MINUTE_OF_HOUR, 2) .appendValue(ChronoField.SECOND_OF_MINUTE, 2) .parseStrict() .appendFraction(ChronoField.MILLI_OF_SECOND, 3, 3, true) .appendZoneId() .toFormatter() .withZone(ZoneId.of("Z")); }
@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(dataProvider="resolveOneNoChange") public void test_resolveOneNoChange(TemporalField field1, long value1) { 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); assertEquals(accessor.isSupported(field1), true); assertEquals(accessor.getLong(field1), value1); }
@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(dataProvider="print_localized") public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) { OffsetDateTime odt = OffsetDateTime.of(ldt, offset); ZonedDateTime zdt = ldt.atZone(offset); DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style) .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); if (style == TextStyle.FULL) { f = new DateTimeFormatterBuilder().appendPattern("ZZZZ") .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); f = new DateTimeFormatterBuilder().appendPattern("OOOO") .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); } if (style == TextStyle.SHORT) { f = new DateTimeFormatterBuilder().appendPattern("O") .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); } }
@Override public Movie createModelFromElement(Node node, String provenanceInfo) { String id = getValueFromChildElement(node, "id"); // create the object with id and provenance information Movie movie = new Movie(id, provenanceInfo); // fill the attributes movie.setTitle(getValueFromChildElement(node, "title")); movie.setDirector(getValueFromChildElement(node, "director")); // convert the date string into a DateTime object try { String date = getValueFromChildElement(node, "date"); if (date != null && !date.isEmpty()) { DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(Locale.ENGLISH); LocalDateTime dt = LocalDateTime.parse(date, formatter); movie.setDate(dt); } } catch (Exception e) { e.printStackTrace(); } // load the list of actors List<Actor> actors = getObjectListFromChildElement(node, "actors", "actor", new ActorXMLReader(), provenanceInfo); movie.setActors(actors); return movie; }
@Test public void test_resolverFields_selectOneDateResolveYMD() throws Exception { DateTimeFormatter base = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).appendLiteral('-') .appendValue(DAY_OF_MONTH).appendLiteral('-').appendValue(DAY_OF_YEAR).toFormatter(); DateTimeFormatter f = base.withResolverFields(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH); try { base.parse("2012-6-30-321", LocalDate::from); // wrong day-of-year fail(); } catch (DateTimeException ex) { // expected, fails as it produces two different dates } LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from); // ignored day-of-year assertEquals(parsed, LocalDate.of(2012, 6, 30)); }
@Test(dataProvider="lenientOffsetAdjacentParseValidPatternData") public void test_lenient_offset_adjacentValidPattern_parse(String pattern, String offset, int offsetSeconds) { TemporalAccessor tmp = new DateTimeFormatterBuilder().parseLenient() .appendOffset(pattern, "Z").appendValue(HOUR_OF_DAY, 2).toFormatter().parse(offset + "12"); assertEquals(tmp.get(OFFSET_SECONDS), offsetSeconds); assertEquals(tmp.get(HOUR_OF_DAY), 12); }
@Test(dataProvider="FieldPatterns") public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) { WeekFields weekDef = WeekFields.of(locale); TemporalField field = null; switch(pattern.charAt(0)) { case 'c' : case 'e' : field = weekDef.dayOfWeek(); break; case 'w': field = weekDef.weekOfWeekBasedYear(); break; case 'W': field = weekDef.weekOfMonth(); break; case 'Y': field = weekDef.weekBasedYear(); break; default: throw new IllegalStateException("bad format letter from pattern"); } 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"); long value = parsed.getLong(field); assertEquals(value, expectedValue, "Value incorrect for " + field); } }
@Test(dataProvider = "parseLenientQuarter") public void test_parse_parseLenientQuarter_LENIENT(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.LENIENT); LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, expected); }
@Test(dataProvider = "resolverStyle") public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) { DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); builder.appendValue(ChronoField.YEAR_OF_ERA); builder.appendLiteral("/"); builder.appendValue(ChronoField.MONTH_OF_YEAR); builder.appendLiteral("/"); builder.appendValue(ChronoField.DAY_OF_MONTH); Map<Long, String> eraMap = new HashMap<Long, String>(); eraMap.put(1L, "CE"); eraMap.put(0L, "BCE"); DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter(); DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter(); formatter = formatter.withResolverStyle(style); if (expectedEx == null) { TemporalAccessor accessor = formatter.parse(str); assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year); assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month); assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day); } else { try { formatter.parse(str); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@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); }