Search in sources :

Example 86 with DateTimeFormatter

use of java.time.format.DateTimeFormatter 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 87 with DateTimeFormatter

use of java.time.format.DateTimeFormatter in project jdk8u_jdk by JetBrains.

the class TestNonIsoFormatter method test_parseInvalidText.

@Test(dataProvider = "invalid_text", expectedExceptions = DateTimeParseException.class)
public void test_parseInvalidText(String text) {
    DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withChronology(JAPANESE).withLocale(Locale.JAPANESE);
    dtf.parse(text);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 88 with DateTimeFormatter

use of java.time.format.DateTimeFormatter 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 89 with DateTimeFormatter

use of java.time.format.DateTimeFormatter 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 90 with DateTimeFormatter

use of java.time.format.DateTimeFormatter in project jdk8u_jdk by JetBrains.

the class TestTextParser method test_parseDayOfWeekText.

@Test(dataProvider = "parseDayOfWeekText")
public void test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected) {
    DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
    ParsePosition pos = new ParsePosition(0);
    assertEquals(DayOfWeek.from(formatter.parse(input, pos)), expected);
    assertEquals(pos.getIndex(), input.length());
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Aggregations

DateTimeFormatter (java.time.format.DateTimeFormatter)905 Test (org.junit.Test)297 Test (org.testng.annotations.Test)272 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)229 TemporalAccessor (java.time.temporal.TemporalAccessor)205 LocalDate (java.time.LocalDate)142 ParsePosition (java.text.ParsePosition)94 LocalDateTime (java.time.LocalDateTime)92 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)85 ZonedDateTime (java.time.ZonedDateTime)71 Instant (java.time.Instant)53 DateTimeParseException (java.time.format.DateTimeParseException)50 ZoneId (java.time.ZoneId)39 TemporalField (java.time.temporal.TemporalField)39 WeekFields (java.time.temporal.WeekFields)36 AbstractTCKTest (tck.java.time.AbstractTCKTest)36 DateTimeException (java.time.DateTimeException)34 IOException (java.io.IOException)26 Format (java.text.Format)26 LocalTime (java.time.LocalTime)25