我想发送 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);
但我得到了异常(exception):
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](默认任务3)java.time.format.DateTimeParseException:无法解析文本'2019-03-19T23:15:14.000Z':无法从 TemporalAccessor 获取 LocalDateTime:{NanoOfSecond=0、InstantSeconds=1553037314、MicroOfSecond=0、MilliOfSecond=0}、java.time.format.Parsed 类型的 ISO
最佳答案
这是因为根据您的 DateTimeFormatter
start_date 无效。不希望在日期的最后出现 Z
。
您有 2 个选择。
- 从
start_date
和end_date
中删除Z
字符。 - 修改
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 - 无法解析,在索引 23 处找到未解析的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55088188/