Search in sources :

Example 6 with TemporalAccessor

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

the class TestReducedParser method test_reducedWithLateChronoChange.

@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));
}
Also used : ThaiBuddhistDate(java.time.chrono.ThaiBuddhistDate) 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 7 with TemporalAccessor

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

the class TestReducedParser method test_reducedWithChronoYearOfEra.

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

Example 8 with TemporalAccessor

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

the class TestReducedParser method test_parseLenient.

//-----------------------------------------------------------------------
// Parsing tests for lenient mode
//-----------------------------------------------------------------------
@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);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 9 with TemporalAccessor

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

the class TestReducedParser method test_parseAllLenient.

@Test(dataProvider = "ParseAll")
public void test_parseAllLenient(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 10 with TemporalAccessor

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

the class TestReducedParser method test_parseAdjacent.

@Test(dataProvider = "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(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
        assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
        assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
    }
}
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)

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