Search in sources :

Example 96 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_resolverFields_ignoreCrossCheck.

@Test
public void test_resolverFields_ignoreCrossCheck() throws Exception {
    DateTimeFormatter base = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral('-').appendValue(DAY_OF_YEAR).appendLiteral('-').appendValue(DAY_OF_WEEK).toFormatter();
    DateTimeFormatter f = base.withResolverFields(YEAR, DAY_OF_YEAR);
    try {
        // wrong day-of-week
        base.parse("2012-321-1", LocalDate::from);
        fail();
    } catch (DateTimeException ex) {
    // expected, should fail in cross-check of day-of-week
    }
    // ignored wrong day-of-week
    LocalDate parsed = f.parse("2012-321-1", LocalDate::from);
    assertEquals(parsed, LocalDate.of(2012, 11, 16));
}
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 97 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_format_withChronology_nonChronoFieldMapLink.

@Test
public void test_format_withChronology_nonChronoFieldMapLink() {
    TemporalAccessor temporal = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return field == IsoFields.WEEK_BASED_YEAR;
        }

        @Override
        public long getLong(TemporalField field) {
            if (field == IsoFields.WEEK_BASED_YEAR) {
                return 2345;
            }
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
        }
    };
    DateTimeFormatter test = new DateTimeFormatterBuilder().appendValue(IsoFields.WEEK_BASED_YEAR, 4).toFormatter(Locale.ENGLISH).withChronology(IsoChronology.INSTANCE);
    String result = test.format(temporal);
    assertEquals(result, "2345");
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 98 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_resolverFields_listOfOneNull.

@Test
public void test_resolverFields_listOfOneNull() throws Exception {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).toFormatter().withResolverFields((TemporalField) null);
    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)

Example 99 with DateTimeFormatterBuilder

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

the class TCKInstantPrinterParser method test_print_digits.

@Test(dataProvider = "printDigits")
public void test_print_digits(int fractionalDigits, long instantSecs, int nano, String expected) {
    Instant instant = Instant.ofEpochSecond(instantSecs, nano);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendInstant(fractionalDigits).toFormatter();
    assertEquals(f.format(instant), expected);
}
Also used : Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 100 with DateTimeFormatterBuilder

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

the class TCKInstantPrinterParser method test_parse_leapSecond.

@Test
public void test_parse_leapSecond() {
    Instant expected = OffsetDateTime.of(1970, 2, 3, 23, 59, 59, 123456789, ZoneOffset.UTC).toInstant();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendInstant(-1).toFormatter();
    for (ResolverStyle style : ResolverStyle.values()) {
        TemporalAccessor parsed = f.withResolverStyle(style).parse("1970-02-03T23:59:60.123456789Z");
        assertEquals(parsed.query(Instant::from), expected);
        assertEquals(parsed.query(DateTimeFormatter.parsedExcessDays()), Period.ZERO);
        assertEquals(parsed.query(DateTimeFormatter.parsedLeapSecond()), Boolean.TRUE);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) Instant(java.time.Instant) ResolverStyle(java.time.format.ResolverStyle) 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