Search in sources :

Example 31 with DateTimeFormatter

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

the class TCKZonedDateTime method factory_parse_formatter_nullText.

@Test(expectedExceptions = NullPointerException.class)
public void factory_parse_formatter_nullText() {
    DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
    ZonedDateTime.parse((String) null, f);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 32 with DateTimeFormatter

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

the class TCKDateTimeFormatters method test_pattern_String.

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
@Test
public void test_pattern_String() {
    DateTimeFormatter test = DateTimeFormatter.ofPattern("d MMM yyyy");
    Locale fmtLocale = Locale.getDefault(Locale.Category.FORMAT);
    assertEquals(test.format(LocalDate.of(2012, 6, 30)), "30 " + Month.JUNE.getDisplayName(TextStyle.SHORT, fmtLocale) + " 2012");
    assertEquals(test.getLocale(), fmtLocale, "Locale.Category.FORMAT");
}
Also used : Locale(java.util.Locale) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 33 with DateTimeFormatter

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

the class TCKDateTimeFormatterBuilder method test_parseDefaulting_yearOptionalMonthOptionalDay.

@Test
public void test_parseDefaulting_yearOptionalMonthOptionalDay() {
    DateTimeFormatter f = builder.appendValue(YEAR).optionalStart().appendLiteral('-').appendValue(MONTH_OF_YEAR).optionalStart().appendLiteral('-').appendValue(DAY_OF_MONTH).optionalEnd().optionalEnd().parseDefaulting(MONTH_OF_YEAR, 1).parseDefaulting(DAY_OF_MONTH, 1).toFormatter();
    assertEquals(f.parse("2012", LocalDate::from), LocalDate.of(2012, 1, 1));
    assertEquals(f.parse("2012-6", LocalDate::from), LocalDate.of(2012, 6, 1));
    assertEquals(f.parse("2012-6-30", LocalDate::from), LocalDate.of(2012, 6, 30));
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 34 with DateTimeFormatter

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

the class TCKDateTimeFormatterBuilder method test_appendOffset_parse.

@Test(dataProvider = "offsetPatterns")
public void test_appendOffset_parse(String pattern, int h, int m, int s, String expected) throws Exception {
    builder.appendOffset(pattern, "Z");
    DateTimeFormatter f = builder.toFormatter();
    ZoneOffset parsed = f.parse(expected, ZoneOffset::from);
    assertEquals(f.format(parsed), expected);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Example 35 with DateTimeFormatter

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

the class TCKDateTimeFormatterBuilder method test_adjacent_strict_fractionFollows.

//-----------------------------------------------------------------------
@Test
public void test_adjacent_strict_fractionFollows() throws Exception {
    // succeeds because hour/min are fixed width
    DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 0, 3, false).toFormatter(Locale.UK);
    ParsePosition pp = new ParsePosition(0);
    TemporalAccessor parsed = f.parseUnresolved("1230567", pp);
    assertEquals(pp.getErrorIndex(), -1);
    assertEquals(pp.getIndex(), 7);
    assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
    assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
    assertEquals(parsed.getLong(NANO_OF_SECOND), 567_000_000L);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Aggregations

DateTimeFormatter (java.time.format.DateTimeFormatter)908 Test (org.junit.Test)300 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)93 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)85 ZonedDateTime (java.time.ZonedDateTime)71 Instant (java.time.Instant)53 DateTimeParseException (java.time.format.DateTimeParseException)52 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