Search in sources :

Example 31 with DateTimeParseException

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

the class TCKDateTimeParseResolver method test_resolveFourToTime.

@Test(dataProvider = "resolveFourToTime")
public void test_resolveFourToTime(ResolverStyle style, long hour, long min, long sec, long nano, LocalTime expectedTime, Period excessPeriod) {
    DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(HOUR_OF_DAY, hour).parseDefaulting(MINUTE_OF_HOUR, min).parseDefaulting(SECOND_OF_MINUTE, sec).parseDefaulting(NANO_OF_SECOND, nano).toFormatter();
    ResolverStyle[] styles = (style != null ? new ResolverStyle[] { style } : ResolverStyle.values());
    for (ResolverStyle s : styles) {
        if (expectedTime != null) {
            TemporalAccessor accessor = f.withResolverStyle(s).parse("");
            assertEquals(accessor.query(TemporalQueries.localDate()), null, "ResolverStyle: " + s);
            assertEquals(accessor.query(TemporalQueries.localTime()), expectedTime, "ResolverStyle: " + s);
            assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), excessPeriod, "ResolverStyle: " + s);
        } else {
            try {
                f.withResolverStyle(style).parse("");
                fail();
            } catch (DateTimeParseException ex) {
            // expected
            }
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) ResolverStyle(java.time.format.ResolverStyle) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 32 with DateTimeParseException

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

the class TCKDateTimeParseResolver method test_resolveMinuteOfDay.

@Test(dataProvider = "resolveMinuteOfDay")
public void test_resolveMinuteOfDay(ResolverStyle style, long value, Integer expectedMinute, int expectedDays) {
    String str = Long.toString(value);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(MINUTE_OF_DAY).toFormatter();
    if (expectedMinute != null) {
        TemporalAccessor accessor = f.withResolverStyle(style).parse(str);
        assertEquals(accessor.query(TemporalQueries.localDate()), null);
        assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.ofSecondOfDay(expectedMinute * 60));
        assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), Period.ofDays(expectedDays));
    } else {
        try {
            f.withResolverStyle(style).parse(str);
            fail();
        } catch (DateTimeParseException ex) {
        // expected
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 33 with DateTimeParseException

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

the class TCKDateTimeParseResolver method test_resolveAmPm.

@Test(dataProvider = "resolveAmPm")
public void test_resolveAmPm(ResolverStyle style, long value, Integer expectedValue) {
    String str = Long.toString(value);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(AMPM_OF_DAY).toFormatter();
    if (expectedValue != null) {
        TemporalAccessor accessor = f.withResolverStyle(style).parse(str);
        assertEquals(accessor.query(TemporalQueries.localDate()), null);
        assertEquals(accessor.query(TemporalQueries.localTime()), null);
        assertEquals(accessor.isSupported(AMPM_OF_DAY), true);
        assertEquals(accessor.getLong(AMPM_OF_DAY), expectedValue.longValue());
    } else {
        try {
            f.withResolverStyle(style).parse(str);
            fail();
        } catch (DateTimeParseException ex) {
        // expected
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 34 with DateTimeParseException

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

the class TCKIsoFields method test_parse_parseLenientWeek_SMART.

@Test(dataProvider = "parseLenientWeek")
public void test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart) {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':').appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(ResolverStyle.SMART);
    if (smart) {
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, expected);
    } else {
        try {
            LocalDate.parse(str, f);
            fail("Should have failed");
        } catch (DateTimeParseException ex) {
        // expected
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 35 with DateTimeParseException

use of java.time.format.DateTimeParseException in project jmeter by apache.

the class TimeShift method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String dateString;
    LocalDateTime localDateTimeToShift = LocalDateTime.now(systemDefaultZoneID);
    DateTimeFormatter formatter = null;
    if (!StringUtils.isEmpty(format)) {
        try {
            formatter = dateTimeFormatterCache.get(format, key -> createFormatter((String) key));
        } catch (IllegalArgumentException ex) {
            // $NON-NLS-1$
            log.error("Format date pattern '{}' is invalid (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)", format, ex);
            return "";
        }
    }
    if (!dateToShift.isEmpty()) {
        try {
            if (formatter != null) {
                localDateTimeToShift = LocalDateTime.parse(dateToShift, formatter);
            } else {
                localDateTimeToShift = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateToShift)), ZoneId.systemDefault());
            }
        } catch (DateTimeParseException | NumberFormatException ex) {
            // $NON-NLS-1$
            log.error("Failed to parse the date '{}' to shift", dateToShift, ex);
        }
    }
    // Check amount value to shift
    if (!StringUtils.isEmpty(amountToShift)) {
        try {
            Duration duration = Duration.parse(amountToShift);
            localDateTimeToShift = localDateTimeToShift.plus(duration);
        } catch (DateTimeParseException ex) {
            // $NON-NLS-1$
            log.error("Failed to parse the amount duration '{}' to shift (see https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-) ", amountToShift, ex);
        }
    }
    if (formatter != null) {
        dateString = localDateTimeToShift.format(formatter);
    } else {
        ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(localDateTimeToShift);
        dateString = String.valueOf(localDateTimeToShift.toInstant(offset).toEpochMilli());
    }
    if (!StringUtils.isEmpty(variableName)) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // vars will be null on TestPlan
            vars.put(variableName, dateString);
        }
    }
    return dateString;
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) JMeterUtils(org.apache.jmeter.util.JMeterUtils) Arrays(java.util.Arrays) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) Collection(java.util.Collection) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) SampleResult(org.apache.jmeter.samplers.SampleResult) Instant(java.time.Instant) Cache(com.github.benmanes.caffeine.cache.Cache) StringUtils(org.apache.commons.lang3.StringUtils) ZoneId(java.time.ZoneId) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Year(java.time.Year) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) Sampler(org.apache.jmeter.samplers.Sampler) DateTimeParseException(java.time.format.DateTimeParseException) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset)

Aggregations

DateTimeParseException (java.time.format.DateTimeParseException)51 DateTimeFormatter (java.time.format.DateTimeFormatter)17 LocalDate (java.time.LocalDate)15 Test (org.testng.annotations.Test)14 TemporalAccessor (java.time.temporal.TemporalAccessor)13 LocalDateTime (java.time.LocalDateTime)11 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)10 Instant (java.time.Instant)7 Duration (java.time.Duration)5 ZonedDateTime (java.time.ZonedDateTime)5 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)5 Period (java.time.Period)4 LocalTime (java.time.LocalTime)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 DataflowException (edu.uci.ics.texera.api.exception.DataflowException)2 ParseException (java.text.ParseException)2 DateTimeException (java.time.DateTimeException)2 ZoneId (java.time.ZoneId)2 ResolverStyle (java.time.format.ResolverStyle)2 Temporal (java.time.temporal.Temporal)2