本文介绍了为什么在尝试解析当前 LocalDateTime 时会出现解析异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义格式化程序解析 LocalDate,但它引发了以下错误.我究竟做错了什么?

I am trying to parse a LocalDate with a custom formatter but it is throwing following error. What am I doing wrong?

错误:

//本地日期时间现在值为 2019-09-19T14:42:23.837java.time.format.DateTimeParseException:无法在索引 10 处解析文本2019-09-19T14:42:23.837"

代码:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        LocalDateTime timeStamp = LocalDateTime.parse(LocalDateTime.now().toString(), formatter);

推荐答案

您可以将其更改为 ISO_DATE_TIME 这会给您T"

You can change it to ISO_DATE_TIME which gives you the "T"

DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

        LocalDateTime timeStamp=  LocalDateTime.parse(LocalDateTime.now().toString(),formatter);
        System.out.println(timeStamp);

这篇关于为什么在尝试解析当前 LocalDateTime 时会出现解析异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 16:46