【问题标题】:could not be parsed, unparsed text found at index 23无法解析,在索引 23 处找到未解析的文本
【发布时间】:2019-03-10 13:30:08
【问题描述】:

我想发送 2 个日期到 Spring 休息点。我试过这个:

start_date=2019-03-03T21:25:24.000Z&end_date=2019-03-03T21:25:29.000Z

休息终点:

    @GetMapping("/terminals")
    public ResponseEntity<Map<Integer, List<TopTerminalsDTO>>> getTopTerminals(
            @RequestParam(value = "start_date", required = true) String start_date,
            @RequestParam(value = "end_date", required = true) String end_date) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
        LocalDateTime start_dateTime = LocalDateTime.parse(start_date, formatter);
        LocalDateTime end_dateTime = LocalDateTime.parse(end_date, formatter);

        final List<PaymentTransactionsDailyFacts> list = dashboardService.findTop_Terminals(start_dateTime, end_dateTime);

但我得到了例外:

2019-03-10 13:22:46,677 INFO  [stdout] (default task-1) 13:22:46.676 [default task-1] ERROR o.s.b.w.s.support.ErrorPageFilter - Forwarding to error page from request [/dashboard/terminals] due to exception [Text '2019-03-03T21:22:39.000Z' could not be parsed, unparsed text found at index 23]
2019-03-10 13:22:46,678 INFO  [stdout] (default task-1) java.time.format.DateTimeParseException: Text '2019-03-03T21:22:39.000Z' could not be parsed, unparsed text found at index 23
2019-03-10 13:22:46,678 INFO  [stdout] (default task-1)     at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
2019-03-10 13:22:46,678 INFO  [stdout] (default task-1)     at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
2019-03-10 13:22:46,679 INFO  [stdout] (default task-1)     at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
2019-03-10 13:22:46,679 INFO  [stdout] (default task-1)     at deployment.datalis_admin.war//org.datalis.admin.backend.restapi.DashboardController.getTopTerminals(DashboardController.java:87)
2019-03-10 13:22:46,679 INFO  [stdout] (default task-1)     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

你知道我该如何解决这个问题吗?

编辑:

LocalDateTime start_dateTime = LocalDateTime.parse(start_date, DateTimeFormatter.ISO_INSTANT);
        LocalDateTime end_dateTime = LocalDateTime.parse(end_date, DateTimeFormatter.ISO_INSTANT);

我得到:23:15:21,554 INFO [stdout] (default task-3) java.time.format.DateTimeParseException: Text '2019-03-19T23:15:14.000Z' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1553037314, MicroOfSecond=0, MilliOfSecond=0},ISO of type java.time.format.Parsed

【问题讨论】:

    标签: java spring


    【解决方案1】:

    这是因为根据你的DateTimeFormatter start_date 是无效的。不期望在日期的最后有Z

    你有两个选择。

    1. 从您的start_dateend_date 中删除Z 字符。
    2. 修改 DateTimeFormatter 以也接受 ZoneOffset。

    例子:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    

    您还可以决定是否不需要 start_date 中的几分之一秒。如果没有,你可以使用这个技巧:

    LocalDateTime start_dateTime = LocalDateTime.parse("2019-03-03T21:25:24Z", DateTimeFormatter.ISO_INSTANT);
    

    【讨论】:

    • 我收到java.time.format.DateTimeParseException: Text '2019-03-19T23:15:14.000Z' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1553037314, MicroOfSecond=0, MilliOfSecond=0},ISO of type java.time.format.Parsed
    • 您使用的是哪个代码 sn-p?你选择了什么样的选项?
    • 第二个。
    • 您可以编辑您的问题并使用 EDIT 标题将您的最新代码粘贴到下面吗?
    • 我写了 如果你的 start_date 中的几分之一秒是不必要的。 这意味着你的参数中没有毫秒。您仍然拥有它,因此除非您切换到 2019-03-19T23:15:14Z 而不是 2019-03-19T23:15:14.000Z,否则它对您不起作用。请在您的情况下使用DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    猜你喜欢
    • 2021-06-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2021-09-20
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多