Search in sources :

Example 11 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TestNonIsoFormatter method test_parseLocalizedText.

@Test(dataProvider = "format_data")
public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale, ChronoLocalDate expected, String text) {
    DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withChronology(chrono).withLocale(formatLocale).withDecimalStyle(DecimalStyle.of(numberingLocale));
    TemporalAccessor temporal = dtf.parse(text);
    ChronoLocalDate date = chrono.date(temporal);
    assertEquals(date, expected);
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 12 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TestNumberParser method test_parse_textField.

@Test(dataProvider = "parseData")
public void test_parse_textField(int minWidth, int maxWidth, SignStyle signStyle, int subsequentWidth, String text, int pos, int expectedPos, long expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatter dtf = getFormatter(DAY_OF_WEEK, minWidth, maxWidth, signStyle);
    if (subsequentWidth > 0) {
        // hacky, to reserve space
        dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withDecimalStyle(decimalStyle);
    }
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertTrue(subsequentWidth >= 0);
        assertEquals(ppos.getIndex(), expectedPos + subsequentWidth);
        assertEquals(parsed.getLong(DAY_OF_WEEK), expectedValue);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 13 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_reducedWithLateChronoChangeTwice.

@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));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 14 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_parseLenient_baseDate.

@Test(dataProvider = "ParseLenientSensitive")
public void test_parseLenient_baseDate(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 = getFormatterBaseDate(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);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 15 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_parseStrict.

//-----------------------------------------------------------------------
// Parsing tests for strict mode
//-----------------------------------------------------------------------
@Test(dataProvider = "ParseLenientSensitive")
public void test_parseStrict(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos, Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Aggregations

TemporalAccessor (java.time.temporal.TemporalAccessor)217 Test (org.testng.annotations.Test)189 DateTimeFormatter (java.time.format.DateTimeFormatter)101 ParsePosition (java.text.ParsePosition)82 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)61 DateTimeException (java.time.DateTimeException)25 DateTimeParseException (java.time.format.DateTimeParseException)21 TemporalField (java.time.temporal.TemporalField)13 LocalDate (java.time.LocalDate)11 Instant (java.time.Instant)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 Chronology (java.time.chrono.Chronology)5 ResolverStyle (java.time.format.ResolverStyle)5 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)4 IsoChronology (java.time.chrono.IsoChronology)4 Format (java.text.Format)3 LocalTime (java.time.LocalTime)3 ZonedDateTime (java.time.ZonedDateTime)3 HijrahChronology (java.time.chrono.HijrahChronology)3 JapaneseChronology (java.time.chrono.JapaneseChronology)3