Search in sources :

Example 41 with DateTimeFormatterBuilder

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

the class TestReducedPrinter method test_printAdjacent.

@Test(dataProvider = "PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
    builder = new DateTimeFormatterBuilder();
    builder.appendPattern(pattern);
    DateTimeFormatter dtf = builder.toFormatter();
    LocalDate ld = LocalDate.of(year, month, day);
    String actual = dtf.format(ld);
    assertEquals(actual, text, "formatter output: " + dtf);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 42 with DateTimeFormatterBuilder

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

the class TestReducedParser method test_reducedWithLateChronoChangeTwice.

@Test
public void test_reducedWithLateChronoChangeTwice() {
    DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1)).appendLiteral(" ").appendChronologyId().appendLiteral(" ").appendChronologyId().toFormatter();
    int expected = 2044;
    String input = "44 ThaiBuddhist ISO";
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos);
    assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
    int actual = parsed.get(YEAR);
    assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", parsed.query(TemporalQueries.chronology()), input));
}
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 43 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project nifi by apache.

the class TimeAdapter method unmarshal.

@Override
public Date unmarshal(String date) throws Exception {
    final LocalDateTime now = LocalDateTime.now();
    final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_TIME_FORMAT).parseDefaulting(ChronoField.YEAR, now.getYear()).parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue()).parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth()).parseDefaulting(ChronoField.MILLI_OF_SECOND, 0).toFormatter(Locale.US);
    final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser);
    return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now)));
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder)

Example 44 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project nifi by apache.

the class TimezoneAdapter method unmarshal.

@Override
public Date unmarshal(String date) throws Exception {
    final LocalDateTime now = LocalDateTime.now();
    final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_DATE_TIME_FORMAT).parseDefaulting(ChronoField.YEAR, now.getYear()).parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue()).parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth()).parseDefaulting(ChronoField.HOUR_OF_DAY, now.getHour()).parseDefaulting(ChronoField.MINUTE_OF_HOUR, now.getMinute()).parseDefaulting(ChronoField.SECOND_OF_MINUTE, now.getSecond()).parseDefaulting(ChronoField.MILLI_OF_SECOND, 0).toFormatter(Locale.US);
    final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser);
    return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now)));
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder)

Example 45 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project streamsx.health by IBMStreams.

the class VinesToObservationParser method readResolve.

public Object readResolve() throws ObjectStreamException {
    formatter = new DateTimeFormatterBuilder().appendPattern(DATE_TIME_PATTERN).toFormatter(Locale.ENGLISH);
    isMappingEnabled = Boolean.parseBoolean(mappingEnabledSupplier.get());
    try {
        InputStream inputStream = Resources.getResource(MAPPING_FILE).openStream();
        lookupTable = new VinesToStreamsCodeLookupTable(inputStream);
    } catch (Exception e) {
        ObjectStreamException ose = new ObjectStreamException() {

            private static final long serialVersionUID = 1L;
        };
        ose.addSuppressed(e);
        throw ose;
    }
    return this;
}
Also used : InputStream(java.io.InputStream) ObjectStreamException(java.io.ObjectStreamException) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParseException(java.text.ParseException) ObjectStreamException(java.io.ObjectStreamException) DateTimeParseException(java.time.format.DateTimeParseException)

Aggregations

DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)244 DateTimeFormatter (java.time.format.DateTimeFormatter)227 TemporalAccessor (java.time.temporal.TemporalAccessor)115 Test (org.testng.annotations.Test)115 Test (org.junit.Test)104 LocalDate (java.time.LocalDate)77 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)69 TemporalField (java.time.temporal.TemporalField)40 AbstractTCKTest (tck.java.time.AbstractTCKTest)36 WeekFields (java.time.temporal.WeekFields)34 DateTimeException (java.time.DateTimeException)26 ParsePosition (java.text.ParsePosition)25 DateTimeParseException (java.time.format.DateTimeParseException)20 Instant (java.time.Instant)16 ZonedDateTime (java.time.ZonedDateTime)10 ResolverStyle (java.time.format.ResolverStyle)10 LocalDateTime (java.time.LocalDateTime)6 ChronoLocalDate (java.time.chrono.ChronoLocalDate)6 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)6 Chronology (java.time.chrono.Chronology)5