Search in sources :

Example 71 with DateTimeFormatterBuilder

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

the class TCKResolverStyle method test_resolverStyle.

@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);
    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();
    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) HashMap(java.util.HashMap) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) DateTimeException(java.time.DateTimeException) Test(org.testng.annotations.Test)

Example 72 with DateTimeFormatterBuilder

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

the class TCKSignStyle method test_signStyle.

@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style).toFormatter();
    formatter = formatter.withZone(ZoneOffset.UTC);
    if (expectedEx == null) {
        String output = formatter.format(localDate);
        assertEquals(output, expectedStr);
    } else {
        try {
            formatter.format(localDate);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) DateTimeException(java.time.DateTimeException) Test(org.testng.annotations.Test)

Example 73 with DateTimeFormatterBuilder

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

the class TCKOffsetPrinterParser method test_print_localized.

@Test(dataProvider = "print_localized")
public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) {
    OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
    ZonedDateTime zdt = ldt.atZone(offset);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style).toFormatter();
    assertEquals(f.format(odt), expected);
    assertEquals(f.format(zdt), expected);
    assertEquals(f.parse(expected, ZoneOffset::from), offset);
    if (style == TextStyle.FULL) {
        f = new DateTimeFormatterBuilder().appendPattern("ZZZZ").toFormatter();
        assertEquals(f.format(odt), expected);
        assertEquals(f.format(zdt), expected);
        assertEquals(f.parse(expected, ZoneOffset::from), offset);
        f = new DateTimeFormatterBuilder().appendPattern("OOOO").toFormatter();
        assertEquals(f.format(odt), expected);
        assertEquals(f.format(zdt), expected);
        assertEquals(f.parse(expected, ZoneOffset::from), offset);
    }
    if (style == TextStyle.SHORT) {
        f = new DateTimeFormatterBuilder().appendPattern("O").toFormatter();
        assertEquals(f.format(odt), expected);
        assertEquals(f.format(zdt), expected);
        assertEquals(f.parse(expected, ZoneOffset::from), offset);
    }
}
Also used : OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 74 with DateTimeFormatterBuilder

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

the class TCKOffsetPrinterParser method test_print_pattern_Z.

@Test(dataProvider = "print")
public void test_print_pattern_Z(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
    String pattern = null;
    if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
        ZonedDateTime zdt = ldt.atZone(zone);
        DateTimeFormatter f1 = new DateTimeFormatterBuilder().appendPattern("Z").toFormatter();
        String output1 = f1.format(zdt);
        assertEquals(output1, (expected.equals("Z") ? "+0000" : expected));
        DateTimeFormatter f2 = new DateTimeFormatterBuilder().appendPattern("ZZ").toFormatter();
        String output2 = f2.format(zdt);
        assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
        DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
        String output3 = f3.format(zdt);
        assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
    } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
        ZonedDateTime zdt = ldt.atZone(zone);
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("ZZZZZ").toFormatter();
        String output = f.format(zdt);
        assertEquals(output, expected);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 75 with DateTimeFormatterBuilder

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

the class TCKZoneIdPrinterParser method setUp.

@BeforeMethod
public void setUp() {
    builder = new DateTimeFormatterBuilder();
    pos = new ParsePosition(0);
}
Also used : DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) BeforeMethod(org.testng.annotations.BeforeMethod)

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