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