Search in sources :

Example 61 with DateTimeFormatter

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

the class TCKDateTimeFormatter method test_withLocale.

//-----------------------------------------------------------------------
@Test
public void test_withLocale() {
    DateTimeFormatter base = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    DateTimeFormatter test = base.withLocale(Locale.GERMAN);
    assertEquals(test.getLocale(), Locale.GERMAN);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 62 with DateTimeFormatter

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

the class TCKDateTimeFormatter method test_parse_CharSequence_ParsePosition.

//-----------------------------------------------------------------------
// parse(CharSequence)
//-----------------------------------------------------------------------
@Test
public void test_parse_CharSequence_ParsePosition() {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    ParsePosition pos = new ParsePosition(3);
    TemporalAccessor result = test.parse("XXXONE30XXX", pos);
    assertEquals(pos.getIndex(), 8);
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
    assertEquals(result.isSupported(HOUR_OF_DAY), false);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 63 with DateTimeFormatter

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

the class TCKDateTimeFormatter method test_parseUnresolved_StringParsePosition_duplicateFieldSameValue.

@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);
}
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 64 with DateTimeFormatter

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

the class TCKDateTimeFormatter method test_resolverFields_selectOneDateResolveYD.

@Test
public void test_resolverFields_selectOneDateResolveYD() 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, DAY_OF_YEAR);
    Set<TemporalField> expected = new HashSet<>(Arrays.asList(YEAR, DAY_OF_YEAR));
    // Use set.equals();  testNG comparison of Collections is ordered
    assertTrue(f.getResolverFields().equals(expected), "ResolveFields: " + f.getResolverFields());
    try {
        // wrong month/day-of-month
        base.parse("2012-6-30-321", LocalDate::from);
        fail();
    } catch (DateTimeException ex) {
    // expected, fails as it produces two different dates
    }
    // ignored month/day-of-month
    LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from);
    assertEquals(parsed, LocalDate.of(2012, 11, 16));
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 65 with DateTimeFormatter

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

the class TCKDateTimeFormatter method test_parse_CharSequence_ParsePosition_nullText.

@Test(expectedExceptions = NullPointerException.class)
public void test_parse_CharSequence_ParsePosition_nullText() {
    DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
    test.parse((CharSequence) null, new ParsePosition(0));
}
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