java.time.format.DateTimeParseException:无法在索引3处解析文本 [英] java.time.format.DateTimeParseException: Text could not be parsed at index 3

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

问题描述

我正在使用Java 8解析日期并找出两个日期之间的差异.

I am using Java 8 to parse the the date and find difference between two dates.

这是我的摘录:

String date1 ="01-JAN-2017";
String date2 = "02-FEB-2017";

DateTimeFormatter df = DateTimeFormatter .ofPattern("DD-MMM-YYYY", en);
LocalDate  d1 = LocalDate.parse(date1, df);
LocalDate  d2 = LocalDate.parse(date2, df);

Long datediff = ChronoUnit.DAYS.between(d1,d2);

运行时出现错误:

java.time.format.DateTimeParseException:无法在索引3处解析文本

java.time.format.DateTimeParseException: Text could not be parsed at index 3

推荐答案

以下代码有效.问题是您使用的是"JAN"而不是"Jan". DateTimeFormatter无法识别它.并将模式更改为 "d-MMM-yyyy".

The following code works. The problem is you are using "JAN" instead of "Jan". DateTimeFormatter does not recognize that it seems. and also change the pattern to "d-MMM-yyyy".

  String date1 ="01-Jan-2017";
  String date2 = "02-Feb-2017";

  DateTimeFormatter df = DateTimeFormatter.ofPattern("d-MMM-yyyy");
  LocalDate  d1 = LocalDate.parse(date1, df);
  LocalDate  d2 = LocalDate.parse(date2, df);

  Long datediff = ChronoUnit.DAYS.between(d1,d2);  

来源: https://www .mkyong.com/java8/java-8-how-to-to-convert-string-to-localdate/

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

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