Search in sources :

Example 26 with DateTimeParseException

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

the class TCKDateTimeParseResolver method test_resolveClockHourOfAmPm.

@Test(dataProvider = "resolveClockHourOfAmPm")
public void test_resolveClockHourOfAmPm(ResolverStyle style, long value, Integer expectedValue) {
    String str = Long.toString(value);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(CLOCK_HOUR_OF_AMPM).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(CLOCK_HOUR_OF_AMPM), false);
        assertEquals(accessor.isSupported(HOUR_OF_AMPM), true);
        assertEquals(accessor.getLong(HOUR_OF_AMPM), 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 27 with DateTimeParseException

use of java.time.format.DateTimeParseException in project jabref by JabRef.

the class Date method parse.

/**
     * Try to parse the following formats
     *  - "M/y" (covers 9/15, 9/2015, and 09/2015)
     *  - "MMMM (dd), yyyy" (covers September 1, 2015 and September, 2015)
     *  - "yyyy-MM-dd" (covers 2009-1-15)
     *  - "dd-MM-yyyy" (covers 15-1-2009)
     *  - "d.M.uuuu" (covers 15.1.2015)
     *  - "uuuu.M.d" (covers 2015.1.15)
     * The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
     */
public static Optional<Date> parse(String dateString) {
    Objects.requireNonNull(dateString);
    List<String> formatStrings = Arrays.asList("uuuu-M-d", "uuuu-M", "d-M-uuuu", "M/uu", "M/uuuu", "MMMM d, uuuu", "MMMM, uuuu", "d.M.uuuu", "uuuu.M.d", "uuuu");
    for (String formatString : formatStrings) {
        try {
            TemporalAccessor parsedDate = DateTimeFormatter.ofPattern(formatString).parse(dateString);
            return Optional.of(new Date(parsedDate));
        } catch (DateTimeParseException ignored) {
        // Ignored
        }
    }
    return Optional.empty();
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) LocalDate(java.time.LocalDate)

Example 28 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)

Example 29 with DateTimeParseException

use of java.time.format.DateTimeParseException in project fess by codelibs.

the class UserInfoBhv method toLocalDateTime.

@Override
protected LocalDateTime toLocalDateTime(final Object value) {
    if (value != null) {
        try {
            final Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value.toString()));
            final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            return date;
        } catch (final DateTimeParseException e) {
            logger.debug("Invalid date format: " + value, e);
        }
    }
    return DfTypeUtil.toLocalDateTime(value);
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeParseException(java.time.format.DateTimeParseException) Instant(java.time.Instant)

Aggregations

DateTimeParseException (java.time.format.DateTimeParseException)29 DateTimeFormatter (java.time.format.DateTimeFormatter)15 Test (org.testng.annotations.Test)14 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)10 TemporalAccessor (java.time.temporal.TemporalAccessor)10 LocalDate (java.time.LocalDate)6 Instant (java.time.Instant)5 LocalDateTime (java.time.LocalDateTime)5 Duration (java.time.Duration)2 ResolverStyle (java.time.format.ResolverStyle)2 Matcher (java.util.regex.Matcher)2 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 Conference (com.github.lantoine.lamsadetools.conferences.Conference)1 AddressInfos (com.github.lantoine.lamsadetools.map.AddressInfos)1 GoogleItineraryMap (com.github.lantoine.lamsadetools.map.GoogleItineraryMap)1 UserDetails (com.github.lantoine.lamsadetools.setCoordinates.UserDetails)1 KvDouble (com.torodb.kvdocument.values.KvDouble)1 KvLong (com.torodb.kvdocument.values.KvLong)1 InstantKvInstant (com.torodb.kvdocument.values.heap.InstantKvInstant)1