无法解析,在索引23处找到未解析的文本

时间:2019-03-10 13:30:08

标签: java spring

我想发送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

1 个答案:

答案 0 :(得分:2)

这是因为根据您的DateTimeFormatter,start_date无效。在日期的最后没有Z

您有2个选择。

  1. 从您的Zstart_date中删除end_date个字符。
  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);