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
}
}
}
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;
}
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());
}
}
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));
}
}
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));
}
}
Aggregations