Search in sources :

Example 81 with DateTimeFormatter

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

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

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

Example 84 with DateTimeFormatter

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

the class TestReducedPrinter method test_printAdjacent.

@Test(dataProvider = "PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
    builder = new DateTimeFormatterBuilder();
    builder.appendPattern(pattern);
    DateTimeFormatter dtf = builder.toFormatter();
    LocalDate ld = LocalDate.of(year, month, day);
    String actual = dtf.format(ld);
    assertEquals(actual, text, "formatter output: " + dtf);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 85 with DateTimeFormatter

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

the class TestReducedPrinter method test_minguoChrono_extendedWidth.

public void test_minguoChrono_extendedWidth() throws Exception {
    // ISO 2021 is Minguo 110
    DateTimeFormatter f = getFormatterBaseDate(YEAR, 2, 4, 2021);
    MinguoDate date = MinguoDate.of(109, 6, 30);
    assertEquals(f.format(date), "109");
    date = MinguoDate.of(110, 6, 30);
    assertEquals(f.format(date), "10");
    date = MinguoDate.of(199, 6, 30);
    assertEquals(f.format(date), "99");
    date = MinguoDate.of(200, 6, 30);
    assertEquals(f.format(date), "00");
    date = MinguoDate.of(209, 6, 30);
    assertEquals(f.format(date), "09");
    date = MinguoDate.of(210, 6, 30);
    assertEquals(f.format(date), "210");
}
Also used : MinguoDate(java.time.chrono.MinguoDate) DateTimeFormatter(java.time.format.DateTimeFormatter)

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