java.time.format.DateTimeFormatterBuilder Java Examples

The following examples show how to use java.time.format.DateTimeFormatterBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TCKWeekFields.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoy_strict(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();
    DateTimeFormatter f = new DateTimeFormatterBuilder()
            .appendValue(YEAR).appendLiteral(':')
            .appendValue(woyField).appendLiteral(':')
            .appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT);
    String str = "2012:0:1";
    try {
        LocalDate date = LocalDate.parse(str, f);
        assertEquals(date.getYear(), 2012);
        assertEquals(date.get(woyField), 0);
        assertEquals(date.get(DAY_OF_WEEK), 1);
    } catch (DateTimeException ex) {
        // expected
    }
}
 
Example #2
Source File: TimeFormatter.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a time formatter that uses the default locale and timezone.
 */
public TimeFormatter() {
    dtf = new DateTimeFormatterBuilder()
            .appendValue(CLOCK_HOUR_OF_AMPM)
            .appendLiteral(':')
            .appendValue(MINUTE_OF_HOUR, 2)
            .optionalStart()
            .appendLiteral(':')
            .appendValue(SECOND_OF_MINUTE, 2)
            .appendLiteral(' ')
            .appendText(ChronoField.AMPM_OF_DAY)
            .optionalStart()
            .appendLiteral(' ')
            .appendOffset("+HH:MM", "+00:00")
            .toFormatter()
            .withLocale(Locale.getDefault())
            .withZone(ZoneId.systemDefault());
}
 
Example #3
Source File: TCKSignStyle.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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));
        }
    }
}
 
Example #4
Source File: TCKWeekFields.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example #5
Source File: TCKWeekFields.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example #6
Source File: TCKDateTimeParseResolver.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
        }
    }
}
 
Example #7
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: TestReducedParser.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_reducedWithLateChronoChange() {
    ThaiBuddhistDate date = ThaiBuddhistDate.of(2543, 1, 1);
    DateTimeFormatter df
            = new DateTimeFormatterBuilder()
                    .appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
                    .appendLiteral(" ")
                    .appendChronologyId()
            .toFormatter();
    int expected = date.get(YEAR);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    assertEquals(pos.getIndex(), input.length(), "Input not parsed completely");
    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));

}
 
Example #9
Source File: TCKWeekFields.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("data_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
    }
}
 
Example #10
Source File: TCKDateTimeParseResolver.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="resolveTwoToField")
public void test_resolveTwoToField(TemporalField field1, long value1,
                                   TemporalField field2, long value2,
                                   TemporalField expectedField1, Long expectedValue1,
                                   TemporalField expectedField2, Long expectedValue2) {
    String str = value1 + " " + value2;
    DateTimeFormatter f = new DateTimeFormatterBuilder()
            .appendValue(field1).appendLiteral(' ')
            .appendValue(field2).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());
    }
}
 
Example #11
Source File: TCKIsoFields.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "parseLenientWeek")
public void test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart) {
    DateTimeFormatter f = new DateTimeFormatterBuilder()
            .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':')
            .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':')
            .appendValue(DAY_OF_WEEK)
            .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
        }
    }
}
 
Example #12
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
        }
    }
}
 
Example #13
Source File: TestReducedParser.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("provider_parseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
    ParsePosition ppos = new ParsePosition(0);
    builder = new DateTimeFormatterBuilder();
    setStrict(strict);
    builder.appendPattern(pattern);
    DateTimeFormatter dtf = builder.toFormatter();

    TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
    assertNotNull(String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf), parsed);
    if (ppos.getErrorIndex() != -1) {
        assertEquals("error case parse position", ppos.getErrorIndex(), parseLen);
    } else {
        assertEquals("parse position", ppos.getIndex(), parseLen);
        assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
        assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
        assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
    }
}
 
Example #14
Source File: TCKLocalizedFieldParser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example #15
Source File: TCKIsoFields.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
        }
    }
}
 
Example #16
Source File: TestReducedParser.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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));

}
 
Example #17
Source File: TCKWeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBYDow_lenient(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().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(dowField);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example #18
Source File: TCKDateTimeParseResolver.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #19
Source File: TCKDateTimeParseResolver.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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
        }
    }
}
 
Example #20
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="resolveFourToTime")
public void test_resolveThreeToTime(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).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.minusNanos(nano), "ResolverStyle: " + s);
            assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), excessPeriod, "ResolverStyle: " + s);
        } else {
            try {
                f.withResolverStyle(style).parse("");
                fail();
            } catch (DateTimeParseException ex) {
                // expected
            }
        }
    }
}
 
Example #21
Source File: TCKIsoFields.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "parseLenientWeek")
public void test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart) {
    DateTimeFormatter f = new DateTimeFormatterBuilder()
            .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':')
            .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':')
            .appendValue(DAY_OF_WEEK)
            .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
        }
    }
}
 
Example #22
Source File: TCKWeekFields.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWoyDow(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(MONTH_OF_YEAR).appendLiteral(':')
                .appendValue(woyField).appendLiteral(':')
                .appendValue(dowField).toFormatter();
        String str = date.getYear() + ":" + date.getMonthValue() + ":" +
                date.get(woyField) + ":" + date.get(dowField);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(" :: " + str + " " + i, parsed, date);

        date = date.plusDays(1);
    }
}
 
Example #23
Source File: TCKWeekFields.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example #24
Source File: TCKIsoFields.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "parseLenientWeek")
public void test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart) {
    DateTimeFormatter f = new DateTimeFormatterBuilder()
            .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':')
            .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':')
            .appendValue(DAY_OF_WEEK)
            .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
        }
    }
}
 
Example #25
Source File: TCKDateTimeFormatter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example #26
Source File: TCKWeekFields.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example #27
Source File: TCKDateTimeParseResolver.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_parse_fromField_InstantSeconds_NanoOfSecond() {
    DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .appendValue(INSTANT_SECONDS).appendLiteral('.').appendValue(NANO_OF_SECOND).toFormatter();
    TemporalAccessor acc = fmt.parse("86402.123456789");
    Instant expected = Instant.ofEpochSecond(86402, 123456789);
    assertEquals(acc.isSupported(INSTANT_SECONDS), true);
    assertEquals(acc.isSupported(NANO_OF_SECOND), true);
    assertEquals(acc.isSupported(MICRO_OF_SECOND), true);
    assertEquals(acc.isSupported(MILLI_OF_SECOND), true);
    assertEquals(acc.getLong(INSTANT_SECONDS), 86402L);
    assertEquals(acc.getLong(NANO_OF_SECOND), 123456789L);
    assertEquals(acc.getLong(MICRO_OF_SECOND), 123456L);
    assertEquals(acc.getLong(MILLI_OF_SECOND), 123L);
    assertEquals(Instant.from(acc), expected);
}
 
Example #28
Source File: TestZoneTextPrinterParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="preferredZones")
public void test_ParseText(String expected, String text, Set<ZoneId> preferred, Locale locale, TextStyle style) {
    DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendZoneText(style, preferred)
                                                          .toFormatter(locale)
                                                          .withDecimalStyle(DecimalStyle.of(locale));

    String ret = fmt.parse(text, TemporalQueries.zone()).getId();

    System.out.printf("[%-5s %s] %24s -> %s(%s)%n",
                      locale.toString(),
                      style == TextStyle.FULL ? " full" :"short",
                      text, ret, expected);

    assertEquals(ret, expected);

}
 
Example #29
Source File: TestReducedParser.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_reducedWithLateChronoChange() {
    ThaiBuddhistDate date = ThaiBuddhistDate.of(2543, 1, 1);
    DateTimeFormatter df
            = new DateTimeFormatterBuilder()
                    .appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
                    .appendLiteral(" ")
                    .appendChronologyId()
            .toFormatter();
    int expected = date.get(YEAR);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    assertEquals(pos.getIndex(), input.length(), "Input not parsed completely");
    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));

}
 
Example #30
Source File: TCKDateTimeParseResolver.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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);
}