Search in sources :

Example 81 with DateTimeException

use of java.time.DateTimeException in project hive by apache.

the class TimestampTZUtil method parse.

public static TimestampTZ parse(String s, ZoneId defaultTimeZone) {
    // need to handle offset with single digital hour, see JDK-8066806
    s = handleSingleDigitHourOffset(s);
    ZonedDateTime zonedDateTime;
    try {
        zonedDateTime = ZonedDateTime.parse(s, FORMATTER);
    } catch (DateTimeParseException e) {
        // try to be more tolerant
        // if the input is invalid instead of incomplete, we'll hit exception here again
        TemporalAccessor accessor = FORMATTER.parse(s);
        // LocalDate must be present
        LocalDate localDate = LocalDate.from(accessor);
        LocalTime localTime;
        ZoneId zoneId;
        try {
            localTime = LocalTime.from(accessor);
        } catch (DateTimeException e1) {
            localTime = DEFAULT_LOCAL_TIME;
        }
        try {
            zoneId = ZoneId.from(accessor);
        } catch (DateTimeException e2) {
            if (defaultTimeZone == null) {
                throw new DateTimeException("Time Zone not available");
            }
            zoneId = defaultTimeZone;
        }
        zonedDateTime = ZonedDateTime.of(localDate, localTime, zoneId);
    }
    if (defaultTimeZone == null) {
        return new TimestampTZ(zonedDateTime);
    }
    return new TimestampTZ(zonedDateTime.withZoneSameInstant(defaultTimeZone));
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) LocalDate(java.time.LocalDate)

Example 82 with DateTimeException

use of java.time.DateTimeException in project knime-core by knime.

the class StringToDateTimeNodeDialog method guessFormat.

private void guessFormat(final String preview) {
    final Collection<String> formats = StringToDateTimeNodeModel.createPredefinedFormats();
    for (final String format : formats) {
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        try {
            ZonedDateTime.parse(preview, formatter);
            m_typeCombobox.setSelectedItem(DateTimeType.ZONED_DATE_TIME);
            m_formatModel.setStringValue(format);
            return;
        } catch (DateTimeException e) {
        }
        try {
            LocalDateTime.parse(preview, formatter);
            m_typeCombobox.setSelectedItem(DateTimeType.LOCAL_DATE_TIME);
            m_formatModel.setStringValue(format);
            return;
        } catch (DateTimeException e) {
        }
        try {
            LocalDate.parse(preview, formatter);
            m_typeCombobox.setSelectedItem(DateTimeType.LOCAL_DATE);
            m_formatModel.setStringValue(format);
            return;
        } catch (DateTimeException e) {
        }
        try {
            LocalTime.parse(preview, formatter);
            m_typeCombobox.setSelectedItem(DateTimeType.LOCAL_TIME);
            m_formatModel.setStringValue(format);
            return;
        } catch (DateTimeException e) {
        }
    }
    m_typeFormatWarningLabel.setText("No suitable format found!");
}
Also used : DateTimeException(java.time.DateTimeException) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 83 with DateTimeException

use of java.time.DateTimeException in project knime-core by knime.

the class DateTimeBasedRowFilterNodeModel method calculateEndDateTime.

/**
 * Calculates the ending point if period/duration or granularity is selected. Otherwise output will be the same as
 * input (endDateTime).
 *
 * @param startDateTime starting point
 * @param endDateTime current ending point
 * @return
 */
private Temporal calculateEndDateTime(final Temporal startDateTime, final Temporal endDateTime) throws ArithmeticException, DateTimeException {
    Temporal end = endDateTime;
    if (m_endSelection.getStringValue().equals(EndMode.Duration.name())) {
        try {
            end = startDateTime.plus(DurationPeriodFormatUtils.parsePeriod(m_periodValueModel.getStringValue()));
        } catch (DateTimeException e1) {
            end = startDateTime.plus(DurationPeriodFormatUtils.parseDuration(m_periodValueModel.getStringValue()));
        }
    }
    if (m_endSelection.getStringValue().equals(EndMode.Numerical.name())) {
        final TemporalAmount amount = Granularity.fromString(m_granularityModel.getStringValue()).getPeriodOrDuration(m_numericalValueModel.getIntValue());
        end = startDateTime.plus(amount);
    }
    return end;
}
Also used : DateTimeException(java.time.DateTimeException) Temporal(java.time.temporal.Temporal) TemporalAmount(java.time.temporal.TemporalAmount)

Example 84 with DateTimeException

use of java.time.DateTimeException in project knime-core by knime.

the class LoopStartWindowNodeModel method getTemporalAmount.

/**
 * Gets the temporal amount from the given string which can be either a Duration or a Period.
 *
 * @param amount string that shall be parsed
 * @return TemporalAmount of the string or {@code null} if it cannot be parsed to Duration or Period.
 */
private TemporalAmount getTemporalAmount(String amount) {
    /* Change milliseconds to seconds to allow parsing. */
    if (amount.endsWith(Unit.MILLISECONDS.getUnitLetter())) {
        String tempAmount = amount.substring(0, amount.length() - Unit.MILLISECONDS.getUnitLetter().length());
        Double temp = Double.parseDouble(tempAmount);
        temp /= 1000;
        amount = temp + Unit.SECONDS.getUnitLetter();
    }
    try {
        return DurationPeriodFormatUtils.parseDuration(amount);
    } catch (DateTimeParseException e) {
        try {
            return DurationPeriodFormatUtils.parsePeriod(amount);
        } catch (DateTimeException e2) {
        }
    }
    return null;
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) DateTimeException(java.time.DateTimeException) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 85 with DateTimeException

use of java.time.DateTimeException in project drools by kiegroup.

the class DayDiffFunction method invoke.

public FEELFnResult<BigDecimal> invoke(@ParameterName("datetime1") String datetime1, @ParameterName("datetime2") String datetime2) {
    if (datetime1 == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime1", "cannot be null"));
    }
    if (datetime2 == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime2", "cannot be null"));
    }
    try {
        TemporalAccessor dt1 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime1).cata(BuiltInType.justNull(), Function.identity());
        TemporalAccessor dt2 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime2).cata(BuiltInType.justNull(), Function.identity());
        return invoke(dt1, dt2);
    } catch (DateTimeException e) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime", "invalid 'date' or 'date and time' parameter", e));
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

Aggregations

DateTimeException (java.time.DateTimeException)88 Test (org.testng.annotations.Test)58 TemporalField (java.time.temporal.TemporalField)48 HashMap (java.util.HashMap)34 LocalDate (java.time.LocalDate)20 TemporalAccessor (java.time.temporal.TemporalAccessor)20 DateTimeFormatter (java.time.format.DateTimeFormatter)13 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)10 MinguoDate (java.time.chrono.MinguoDate)7 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)7 InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)7 MockFieldValue (test.java.time.temporal.MockFieldValue)7 HijrahDate (java.time.chrono.HijrahDate)6 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 JapaneseDate (java.time.chrono.JapaneseDate)5 Chronology (java.time.chrono.Chronology)4 IOException (java.io.IOException)3 LocalTime (java.time.LocalTime)3 WeekFields (java.time.temporal.WeekFields)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3