DateTimeParseException при анализе 01 февраля 25 12.00.00.000000 AM UTC, в чем проблема? ⇐ JAVA

Программисты JAVA общаются здесь
Anonymous
DateTimeParseException при анализе 01 февраля 25 12.00.00.000000 AM UTC, в чем проблема?

Сообщение Anonymous »

У меня есть следующий код Java (версия 8), который анализирует строку даты и времени, которая выглядит как '01-FEB-25 12.00.00.000000 AM UTC'. Ожидаемый результат — ZonedDateTime 2025-02-01T00:00Z[UTC]. Но я продолжаю получать сообщение об ошибке «java.time.format.DateTimeParseException: текст '01-FEB-25 12.00.00.000000 AM UTC' не удалось проанализировать по индексу 3».
Минимальный код для воспроизведения:
String dateTimeInput = "01-FEB-25 12.00.00.000000 AM UTC";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(
"dd-MMM-yy hh.mm.ss.SSSSSS a z", Locale.ENGLISH);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeInput, inputFormatter);

Я также попробовал использовать приведенный ниже форматировщик входных данных, используя DateTimeFormatterBuilder, но он все равно не смог проанализировать входную строку.
На этот раз в сообщении об исключении упоминается другой индекс :

Текст «01-FEB-25 12.00.00.000000 AM UTC» не удалось проанализировать по индексу 32

DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yy hh.mm.ss.SSSSSS a z")
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true)
.optionalEnd()
.appendPattern(" a z") // AM/PM and time zone
.toFormatter(Locale.ENGLISH);

Может ли кто-нибудь обнаружить проблему в моем коде и указать мне правильное направление?
Исходный код
Полный метод преобразует входную строку даты и времени в то, что может понять база данных Oracle SQL:
private String convertDateTime(String dateTimeInput) {
if (csDateTime != null) {
try {
// Incoming date time looks like '01-FEB-25 12.00.00.000000 AM UTC'
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd-MMM-yy hh.mm.ss.SSSSSS a z", Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// Parse the input string into ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeInput, inputFormatter);

// Convert to LocalDateTime in UTC
LocalDateTime dateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();

return dateTime.format(outputFormatter);
} catch (DateTimeParseException e) {
LOG.error("Failed to parse date: {}", dateTimeInput, e);
return null;
}
}
return null;
}

Попытка использования DateTimeFormatterBuilder:
DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yy hh.mm.ss.SSSSSS a z")
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true)
.optionalEnd()
.appendPattern(" a z") // AM/PM and time zone
.toFormatter(Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeInput, inputFormatter);
LocalDateTime dateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
return dateTime.format(outputFormatter);


Подробнее здесь: https://stackoverflow.com/questions/790 ... where-is-t

Вернуться в «JAVA»