当我尝试解析当前的LocalDateTime时,为什么会出现解析异常 [英] Why am I getting a parse exception when I try to parse the current LocalDateTime

查看:293
本文介绍了当我尝试解析当前的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.837 java.time.format.DateTimeParseException:无法在索引10处解析文本'2019-09-19T14:42:23.837'

// The local date time now value is 2019-09-19T14:42:23.837 java.time.format.DateTimeParseException: Text '2019-09-19T14:42:23.837' could not be parsed at index 10

代码:

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时,为什么会出现解析异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆