java.time.format.DateTimeParseException:无法在索引5处解析文本“2016-2-2” [英] java.time.format.DateTimeParseException: Text '2016-2-2' could not be parsed at index 5

查看:886
本文介绍了java.time.format.DateTimeParseException:无法在索引5处解析文本“2016-2-2”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从中创建一个 LocalDate 的实例,导入java.time.LocalDate;
我按照这个

I am trying to make an instance of LocalDate from import java.time.LocalDate; and i follow this

这是我的代码:

     LocalDate sd=  LocalDate.parse("2016-2-2");

我面临错误:

java.time.format.DateTimeParseException: Text '2016-2-2' could not be parsed at index 5
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)






在另一个尝试制作<的实例code> LocalDate ,我试过

LocalDate ed=  new LocalDate("2016-2-4");

但它再次抱怨:

The constructor LocalDate(String) is undefined


推荐答案

您需要使用格式化程序来解析单个字符的日/月字段 java.time.LocalDate

You need to use a formatter to parse single character day/month fields for java.time.LocalDate

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
LocalDate date = LocalDate.parse("2016-2-2", formatter);

这篇关于java.time.format.DateTimeParseException:无法在索引5处解析文本“2016-2-2”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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