Search in sources :

Example 91 with DateTimeFormatterBuilder

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

the class TestReducedParser method test_reducedWithChronoYear.

@Test(dataProvider = "ReducedWithChrono")
public void test_reducedWithChronoYear(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1)).toFormatter().withChronology(chrono);
    int expected = date.get(YEAR);
    String input = df.format(date);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR);
    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 92 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_format_withZone_withChronology.

@Test(dataProvider = "formatWithZoneWithChronology")
public void test_format_withZone_withChronology(Chronology overrideChrono, ZoneId overrideZone, TemporalAccessor temporal, String expected) {
    DateTimeFormatter test = new DateTimeFormatterBuilder().optionalStart().appendValue(YEAR, 4).optionalEnd().appendLiteral(':').optionalStart().appendValue(HOUR_OF_DAY, 2).optionalEnd().appendLiteral(':').optionalStart().appendOffsetId().optionalEnd().appendLiteral(':').optionalStart().appendZoneId().optionalEnd().appendLiteral(':').optionalStart().appendChronologyId().optionalEnd().toFormatter(Locale.ENGLISH).withChronology(overrideChrono).withZone(overrideZone);
    if (expected != null) {
        String result = test.format(temporal);
        assertEquals(result, expected);
    } else {
        try {
            test.format(temporal);
            fail("Formatting should have failed");
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : DateTimeException(java.time.DateTimeException) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 93 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue.

@Test
public void test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue() {
    DateTimeFormatter test = new DateTimeFormatterBuilder().appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).toFormatter();
    ParsePosition pos = new ParsePosition(3);
    TemporalAccessor result = test.parseUnresolved("XXX6-7", pos);
    assertEquals(pos.getIndex(), 3);
    assertEquals(pos.getErrorIndex(), 5);
    assertEquals(result, null);
}
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 94 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_resolverFields_selectOneDateResolveYMD.

//-----------------------------------------------------------------------
@Test
public void test_resolverFields_selectOneDateResolveYMD() throws Exception {
    DateTimeFormatter base = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(DAY_OF_MONTH).appendLiteral('-').appendValue(DAY_OF_YEAR).toFormatter();
    DateTimeFormatter f = base.withResolverFields(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH);
    try {
        // wrong day-of-year
        base.parse("2012-6-30-321", LocalDate::from);
        fail();
    } catch (DateTimeException ex) {
    // expected, fails as it produces two different dates
    }
    // ignored day-of-year
    LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from);
    assertEquals(parsed, LocalDate.of(2012, 6, 30));
}
Also used : DateTimeException(java.time.DateTimeException) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 95 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_resolverFields_emptyList.

@Test
public void test_resolverFields_emptyList() throws Exception {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).toFormatter().withResolverFields();
    TemporalAccessor parsed = f.parse("2012");
    // not in the list of resolverFields
    assertEquals(parsed.isSupported(YEAR), false);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Aggregations

DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)122 Test (org.testng.annotations.Test)115 DateTimeFormatter (java.time.format.DateTimeFormatter)114 TemporalAccessor (java.time.temporal.TemporalAccessor)61 LocalDate (java.time.LocalDate)38 TemporalField (java.time.temporal.TemporalField)20 AbstractTCKTest (tck.java.time.AbstractTCKTest)18 WeekFields (java.time.temporal.WeekFields)17 ParsePosition (java.text.ParsePosition)14 DateTimeException (java.time.DateTimeException)12 DateTimeParseException (java.time.format.DateTimeParseException)10 Instant (java.time.Instant)8 ZonedDateTime (java.time.ZonedDateTime)5 ResolverStyle (java.time.format.ResolverStyle)5 BeforeMethod (org.testng.annotations.BeforeMethod)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)4 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)3 Chronology (java.time.chrono.Chronology)3 HijrahChronology (java.time.chrono.HijrahChronology)3 IsoChronology (java.time.chrono.IsoChronology)3