解析 01-FEB-25 12.00.00.000000 AM UTC 时出现 DateTimeParseException,问题出在哪里?

问题描述 投票:0回答:2

我有以下 Java(版本 8)方法,可以将看起来像

'01-FEB-25 12.00.00.000000 AM UTC'
的输入日期时间字符串解析为 Oracle SQL 数据库可以理解的内容,但我不断收到错误
"java.time.format.DateTimeParseException: Text '01-FEB-25 12.00.00.000000 AM UTC' could not be parsed at index 3"

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);
java datetime java-time datetime-parsing
2个回答
4
投票

您的第一个解决方案不起作用,因为格式化的日期时间区分大小写,因此 FEB 不被接受,但 Feb 可以工作 第二个解决方案不起作用,因为您添加了很多不必要的配置,在这种情况下,预期的字符串应该类似于 01-FEB-25 12.00.00.000000 AM UTC.000000 AM UTC 这是工作代码的示例:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        String input = "01-FEB-25 12.00.00.000000 AM UTC";
        System.out.println(convertDateTime(input));
    }

    private static String convertDateTime(String dateTimeInput) {

            try {
                DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
                        .parseCaseInsensitive()
                        .appendPattern("dd-MMM-yy hh.mm.ss.SSSSSS a z")
                        .toFormatter(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) {
                e.printStackTrace();
                return null;
            }
    }
}

-1
投票

看起来这会成功:

private String convertDateTime(String input) {
        if (csDateTime != null) {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS a z");
                formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

                Date parsedDate = formatter.parse(input);
                return new Timestamp(parsedDate.getTime()).toString();
            } catch (Exception e) {
                LOG.error("Failed to parse date: {}", input, e);
                return null;
            }
        }
        return null;
    }
© www.soinside.com 2019 - 2024. All rights reserved.