Java Code Examples for java.time.format.DateTimeFormatter#parseUnresolved()

The following examples show how to use java.time.format.DateTimeFormatter#parseUnresolved() . 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: TestReducedParser.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("provider_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(String.format("Wrong date parsed, chrono: %s, input: %s", chrono, input),
        actual, expected);

}
 
Example 2
Source File: TestNumberParser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="parseData")
public void test_parse_fresh(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_MONTH, 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_MONTH), expectedValue);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
 
Example 3
Source File: TestReducedParser.java    From openjdk-jdk8u-backup 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 4
Source File: TestReducedParser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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));

}
 
Example 5
Source File: TestNumberParser.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_parseUnresolved_StringParsePosition_parseError() {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = test.parseUnresolved("ONEXXX", pos);
    assertEquals(pos.getIndex(), 0);
    assertEquals(pos.getErrorIndex(), 3);
    assertEquals(result, null);
}
 
Example 7
Source File: TestDateTimeFormatterBuilder.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_appendValueReduced() throws Exception {
    builder.appendValueReduced(YEAR, 2, 2, 2000);
    DateTimeFormatter f = builder.toFormatter();
    assertEquals(f.toString(), "ReducedValue(Year,2,2,2000)");
    TemporalAccessor parsed = f.parseUnresolved("12", new ParsePosition(0));
    assertEquals(parsed.getLong(YEAR), 2012L);
}
 
Example 8
Source File: TestDateTimeFormatterBuilder.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_appendValueReduced_subsequent_parse() throws Exception {
    builder.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL).appendValueReduced(YEAR, 2, 2, 2000);
    DateTimeFormatter f = builder.toFormatter();
    assertEquals(f.toString(), "Value(MonthOfYear,1,2,NORMAL)ReducedValue(Year,2,2,2000)");
    ParsePosition ppos = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("123", ppos);
    assertNotNull(parsed, "Parse failed: " + ppos.toString());
    assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
    assertEquals(parsed.getLong(YEAR), 2023L);
}
 
Example 9
Source File: TCKDateTimeFormatterBuilder.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_strict_fractionFollows_0digit() throws Exception {
    // succeeds because hour/min are fixed width
    DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 0, 3, false).toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("1230", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 4);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}
 
Example 10
Source File: TCKDateTimeFormatterBuilder.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_strict_firstVariableWidth_success() throws Exception {
    // succeeds greedily parsing variable width, then fixed width, to non-numeric Z
    DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("12309Z", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 6);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 123L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 9L);
}
 
Example 11
Source File: TCKDateTimeFormatterBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_strict_firstFixedWidth() throws Exception {
    // succeeds because both number elements are fixed width
    DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("12309", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 5);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}
 
Example 12
Source File: TCKDateTimeFormatter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_parseUnresolved_StringParsePosition_parseError() {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = test.parseUnresolved("ONEXXX", pos);
    assertEquals(pos.getIndex(), 0);
    assertEquals(pos.getErrorIndex(), 3);
    assertEquals(result, null);
}
 
Example 13
Source File: TCKDateTimeFormatterBuilder.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_lenient_fractionFollows_0digit() throws Exception {
    // succeeds because hour/min are fixed width
    DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("1230", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 4);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}
 
Example 14
Source File: TCKDateTimeFormatter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_parseUnresolved_StringParsePosition_duplicateFieldSameValue() {
    DateTimeFormatter test = new DateTimeFormatterBuilder()
            .appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).toFormatter();
    ParsePosition pos = new ParsePosition(3);
    TemporalAccessor result = test.parseUnresolved("XXX6-6", pos);
    assertEquals(pos.getIndex(), 6);
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(result.getLong(MONTH_OF_YEAR), 6);
}
 
Example 15
Source File: TCKDateTimeFormatterBuilder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_strict_firstVariableWidth_fails() throws Exception {
    // fails because literal is a number and variable width parse greedily absorbs it
    DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("12309", pp);
    assertEquals(pp.getErrorIndex(), 5);
    assertEquals(parsed, null);
}
 
Example 16
Source File: TCKDateTimeFormatterBuilder.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_lenient_firstVariableWidth_success() throws Exception {
    // succeeds greedily parsing variable width, then fixed width, to non-numeric Z
    DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("12309Z", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 6);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 123L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 9L);
}
 
Example 17
Source File: TCKDateTimeFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_parseUnresolved_StringParsePosition_parseError() {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor result = test.parseUnresolved("ONEXXX", pos);
    assertEquals(pos.getIndex(), 0);
    assertEquals(pos.getErrorIndex(), 3);
    assertEquals(result, null);
}
 
Example 18
Source File: TCKDateTimeFormatterBuilder.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_strict_firstFixedWidth() throws Exception {
    // succeeds because both number elements are fixed width
    DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("12309", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 5);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}
 
Example 19
Source File: TestDateTimeFormatterBuilder.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_appendValue_subsequent2_parse5() throws Exception {
    builder.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL).appendValue(DAY_OF_MONTH, 2).appendLiteral('4');
    DateTimeFormatter f = builder.toFormatter();
    assertEquals(f.toString(), "Value(MonthOfYear,1,2,NORMAL)Value(DayOfMonth,2)'4'");
    TemporalAccessor parsed = f.parseUnresolved("01234", new ParsePosition(0));
    assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
    assertEquals(parsed.getLong(DAY_OF_MONTH), 23L);
}
 
Example 20
Source File: TCKDateTimeFormatterBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_adjacent_lenient() throws Exception {
    // succeeds because both number elements are fixed width even in lenient mode
    DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("12309", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 5);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}