Search in sources :

Example 36 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKInstantPrinterParser method test_parse_digitsNine.

@Test(dataProvider = "parseDigits")
public void test_parse_digitsNine(long instantSecs, int nano, String input) {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendInstant(9).toFormatter();
    if (input.charAt(input.length() - 11) == '.') {
        Instant expected = Instant.ofEpochSecond(instantSecs, nano);
        assertEquals(f.parse(input, Instant::from), expected);
        assertEquals(f.parse(input).query(DateTimeFormatter.parsedExcessDays()), Period.ZERO);
        assertEquals(f.parse(input).query(DateTimeFormatter.parsedLeapSecond()), Boolean.FALSE);
    } else {
        try {
            f.parse(input, Instant::from);
            fail();
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : DateTimeException(java.time.DateTimeException) Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 37 with DateTimeException

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

the class PrimitiveObjectInspectorUtils method getTimestampFromString.

static Timestamp getTimestampFromString(String s) {
    Timestamp result;
    s = s.trim();
    s = trimNanoTimestamp(s);
    int firstSpace = s.indexOf(' ');
    if (firstSpace < 0) {
        s = s.concat(" 00:00:00");
    }
    try {
        result = Timestamp.valueOf(s);
    } catch (IllegalArgumentException e) {
        // Let's try to parse it as timestamp with time zone and transform
        try {
            result = Timestamp.from(TimestampTZUtil.parse(s).getZonedDateTime().toInstant());
        } catch (DateTimeException e2) {
            result = null;
        }
    }
    return result;
}
Also used : DateTimeException(java.time.DateTimeException) Timestamp(java.sql.Timestamp)

Example 38 with DateTimeException

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

the class StringToDateTimeNodeDialog method formatListener.

/**
 * method for change/action listener of type and date combo boxes.
 */
private boolean formatListener(final String format) {
    // remove all expressions in unquoted square brackets, i.e. removes all optional fields of the format
    final StringBuilder buffer = new StringBuilder();
    int parenthesisCounter = 0;
    int apostropheCounter = 0;
    for (char c : format.toCharArray()) {
        if (c == '\'') {
            apostropheCounter++;
        } else if ((c == '[') && (apostropheCounter % 2 == 0)) {
            parenthesisCounter++;
        } else if ((c == ']') && (apostropheCounter % 2 == 0)) {
            parenthesisCounter--;
        }
        if ((!((c == '[') || (c == ']')) || (apostropheCounter % 2 == 1)) && (parenthesisCounter == 0)) {
            buffer.append(c);
        }
    }
    final String formatNoOptionalFields = buffer.toString();
    // check, if the pattern is valid
    switch((DateTimeType) m_typeCombobox.getSelectedItem()) {
        case LOCAL_DATE:
            {
                try {
                    final LocalDate now1 = LocalDate.now();
                    final DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern(format);
                    LocalDate.parse(now1.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), formatter1);
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.LOCAL_DATE.toString() + " needs a date, but does not support a time, time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        case LOCAL_TIME:
            {
                try {
                    final LocalTime now2 = LocalTime.now();
                    final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(format);
                    LocalTime.parse(now2.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), formatter2);
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.LOCAL_TIME.toString() + " needs a time, but does not support a date, time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        case LOCAL_DATE_TIME:
            {
                try {
                    final LocalDateTime now3 = LocalDateTime.now();
                    LocalDateTime.parse(now3.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), DateTimeFormatter.ofPattern(format));
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.LOCAL_DATE_TIME.toString() + " needs date and time, but does not support a time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        case ZONED_DATE_TIME:
            {
                try {
                    final ZonedDateTime now4 = ZonedDateTime.now();
                    final DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern(format);
                    ZonedDateTime.parse(now4.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), formatter4);
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.ZONED_DATE_TIME.toString() + " needs date, time and a time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        default:
            throw new IllegalStateException("Unhandled date&time type: " + m_typeCombobox.getSelectedItem());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDate(java.time.LocalDate) DateTimeType(org.knime.time.util.DateTimeType) DateTimeException(java.time.DateTimeException) ZonedDateTime(java.time.ZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 39 with DateTimeException

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

the class TimeFunction method invoke.

public FEELFnResult<TemporalAccessor> invoke(@ParameterName("hour") Number hour, @ParameterName("minute") Number minute, @ParameterName("second") Number seconds, @ParameterName("offset") Duration offset) {
    if (hour == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "hour", "cannot be null"));
    }
    if (minute == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "minute", "cannot be null"));
    }
    if (seconds == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "seconds", "cannot be null"));
    }
    try {
        int nanosecs = 0;
        if (seconds instanceof BigDecimal) {
            BigDecimal secs = (BigDecimal) seconds;
            nanosecs = secs.subtract(secs.setScale(0, BigDecimal.ROUND_DOWN)).multiply(NANO_MULT).intValue();
        }
        if (offset == null) {
            return FEELFnResult.ofResult(LocalTime.of(hour.intValue(), minute.intValue(), seconds.intValue(), nanosecs));
        } else {
            return FEELFnResult.ofResult(OffsetTime.of(hour.intValue(), minute.intValue(), seconds.intValue(), nanosecs, ZoneOffset.ofTotalSeconds((int) offset.getSeconds())));
        }
    } catch (DateTimeException e) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "time-parsing exception", e));
    }
}
Also used : DateTimeException(java.time.DateTimeException) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) BigDecimal(java.math.BigDecimal)

Example 40 with DateTimeException

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

the class YearDiffFunction 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