Search in sources :

Example 51 with TemporalAccessor

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

the class TestZoneOffsetParser method test_parse_exactMatch.

@Test(dataProvider = "offsets")
public void test_parse_exactMatch(String pattern, String parse, ZoneOffset expected) throws Exception {
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(pattern, "Z").parseUnresolved(parse, pos);
    assertEquals(pos.getIndex(), parse.length());
    assertParsed(parsed, expected);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 52 with TemporalAccessor

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

the class TestDateTimeBuilderCombinations method test_derive.

@Test(dataProvider = "combine")
public void test_derive(final TemporalField field1, final Number value1, final TemporalField field2, final Number value2, final TemporalField field3, final Number value3, final TemporalField field4, final Number value4, Class<?> query, Object expectedVal) {
    // mock for testing that does not fully comply with TemporalAccessor contract
    TemporalAccessor test = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return field == field1 || field == field2 || field == field3 || field == field4;
        }

        @Override
        public long getLong(TemporalField field) {
            if (field == field1) {
                return value1.longValue();
            }
            if (field == field2) {
                return value2.longValue();
            }
            if (field == field3) {
                return value3.longValue();
            }
            if (field == field4) {
                return value4.longValue();
            }
            throw new DateTimeException("Unsupported");
        }
    };
    String str = "";
    DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
    dtfb.appendValue(field1).appendLiteral('-');
    str += value1 + "-";
    if (field2 != null) {
        dtfb.appendValue(field2).appendLiteral('-');
        str += value2 + "-";
    }
    if (field3 != null) {
        dtfb.appendValue(field3).appendLiteral('-');
        str += value3 + "-";
    }
    if (field4 != null) {
        dtfb.appendValue(field4).appendLiteral('-');
        str += value4 + "-";
    }
    TemporalAccessor parsed = dtfb.toFormatter().parse(str);
    if (query == LocalDate.class) {
        if (expectedVal != null) {
            assertEquals(parsed.query(LocalDate::from), expectedVal);
        } else {
            try {
                parsed.query(LocalDate::from);
                fail();
            } catch (DateTimeException ex) {
            // expected
            }
        }
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 53 with TemporalAccessor

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

the class TestZoneOffsetParser method test_parse_invalid.

@Test(dataProvider = "badOffsets")
public void test_parse_invalid(String pattern, String parse, int expectedPosition) throws Exception {
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(pattern, "Z").parseUnresolved(parse, pos);
    assertEquals(pos.getErrorIndex(), expectedPosition);
    assertEquals(parsed, null);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 54 with TemporalAccessor

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

the class TestZoneOffsetParser method test_parse_exactMatch_EmptyUTC.

@Test(dataProvider = "offsets")
public void test_parse_exactMatch_EmptyUTC(String pattern, String parse, ZoneOffset expected) throws Exception {
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(pattern, "").parseUnresolved(parse, pos);
    assertEquals(pos.getIndex(), parse.length());
    assertParsed(parsed, expected);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 55 with TemporalAccessor

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

the class TestZoneOffsetParser method test_parse_exactMatch_UTC.

//-----------------------------------------------------------------------
public void test_parse_exactMatch_UTC() throws Exception {
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter("+HH:MM:ss", "Z").parseUnresolved("Z", pos);
    assertEquals(pos.getIndex(), 1);
    assertParsed(parsed, ZoneOffset.UTC);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition)

Aggregations

TemporalAccessor (java.time.temporal.TemporalAccessor)235 Test (org.testng.annotations.Test)189 DateTimeFormatter (java.time.format.DateTimeFormatter)103 ParsePosition (java.text.ParsePosition)82 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)61 DateTimeException (java.time.DateTimeException)32 DateTimeParseException (java.time.format.DateTimeParseException)24 LocalDate (java.time.LocalDate)14 TemporalField (java.time.temporal.TemporalField)13 ZonedDateTime (java.time.ZonedDateTime)7 LocalTime (java.time.LocalTime)6 InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)6 Instant (java.time.Instant)5 ZoneId (java.time.ZoneId)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 Chronology (java.time.chrono.Chronology)5 ResolverStyle (java.time.format.ResolverStyle)5 Test (org.junit.Test)5 LocalDateTime (java.time.LocalDateTime)4 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)4