Search in sources :

Example 11 with DateTimeParseException

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

the class Period method parse.

//-----------------------------------------------------------------------
/**
     * Obtains a {@code Period} from a text string such as {@code PnYnMnD}.
     * <p>
     * This will parse the string produced by {@code toString()} which is
     * based on the ISO-8601 period formats {@code PnYnMnD} and {@code PnW}.
     * <p>
     * The string starts with an optional sign, denoted by the ASCII negative
     * or positive symbol. If negative, the whole period is negated.
     * The ASCII letter "P" is next in upper or lower case.
     * There are then four sections, each consisting of a number and a suffix.
     * At least one of the four sections must be present.
     * The sections have suffixes in ASCII of "Y", "M", "W" and "D" for
     * years, months, weeks and days, accepted in upper or lower case.
     * The suffixes must occur in order.
     * The number part of each section must consist of ASCII digits.
     * The number may be prefixed by the ASCII negative or positive symbol.
     * The number must parse to an {@code int}.
     * <p>
     * The leading plus/minus sign, and negative values for other units are
     * not part of the ISO-8601 standard. In addition, ISO-8601 does not
     * permit mixing between the {@code PnYnMnD} and {@code PnW} formats.
     * Any week-based input is multiplied by 7 and treated as a number of days.
     * <p>
     * For example, the following are valid inputs:
     * <pre>
     *   "P2Y"             -- Period.ofYears(2)
     *   "P3M"             -- Period.ofMonths(3)
     *   "P4W"             -- Period.ofWeeks(4)
     *   "P5D"             -- Period.ofDays(5)
     *   "P1Y2M3D"         -- Period.of(1, 2, 3)
     *   "P1Y2M3W4D"       -- Period.of(1, 2, 25)
     *   "P-1Y2M"          -- Period.of(-1, 2, 0)
     *   "-P1Y2M"          -- Period.of(-1, -2, 0)
     * </pre>
     *
     * @param text  the text to parse, not null
     * @return the parsed period, not null
     * @throws DateTimeParseException if the text cannot be parsed to a period
     */
public static Period parse(CharSequence text) {
    Objects.requireNonNull(text, "text");
    Matcher matcher = PATTERN.matcher(text);
    if (matcher.matches()) {
        int negate = ("-".equals(matcher.group(1)) ? -1 : 1);
        String yearMatch = matcher.group(2);
        String monthMatch = matcher.group(3);
        String weekMatch = matcher.group(4);
        String dayMatch = matcher.group(5);
        if (yearMatch != null || monthMatch != null || dayMatch != null || weekMatch != null) {
            try {
                int years = parseNumber(text, yearMatch, negate);
                int months = parseNumber(text, monthMatch, negate);
                int weeks = parseNumber(text, weekMatch, negate);
                int days = parseNumber(text, dayMatch, negate);
                days = Math.addExact(days, Math.multiplyExact(weeks, 7));
                return create(years, months, days);
            } catch (NumberFormatException ex) {
                throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex);
            }
        }
    }
    throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0);
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) Matcher(java.util.regex.Matcher)

Example 12 with DateTimeParseException

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

the class SearchLogBhv 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)

Example 13 with DateTimeParseException

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

the class ClickLogBhv 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)

Example 14 with DateTimeParseException

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

the class FavoriteLogBhv 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)

Example 15 with DateTimeParseException

use of java.time.format.DateTimeParseException in project textdb by TextDB.

the class ComparableMatcher method compareDateTime.

private boolean compareDateTime(Tuple inputTuple) throws DataflowException {
    LocalDateTime dateTime = inputTuple.getField(predicate.getAttributeName(), DateTimeField.class).getValue();
    String compareToString = predicate.getCompareToValue().toString();
    // try to parse the input as date time string first
    try {
        LocalDateTime compareToDateTime = LocalDateTime.parse(compareToString);
        return compareValues(dateTime, compareToDateTime, predicate.getComparisonType());
    } catch (DateTimeParseException e) {
        // if it fails, then try to parse as date time string and compare on date
        try {
            LocalDate compareToDate = LocalDate.parse(compareToString);
            return compareValues(dateTime.toLocalDate(), compareToDate, predicate.getComparisonType());
        } catch (DateTimeParseException e2) {
            throw new DataflowException("Unable to parse date or time: " + compareToString);
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeParseException(java.time.format.DateTimeParseException) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) DateTimeField(edu.uci.ics.texera.api.field.DateTimeField) LocalDate(java.time.LocalDate)

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