Java 8 java.time.format.DateTimeParseException:无法在索引3处分析文本
我正在使用Java8解析日期,并找到两个日期之间的差异 以下是我的片段:Java 8 java.time.format.DateTimeParseException:无法在索引3处分析文本,java-8,java-time,date-parsing,Java 8,Java Time,Date Parsing,我正在使用Java8解析日期,并找到两个日期之间的差异 以下是我的片段: 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
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处分析文本
下面的代码可以工作。问题是你用的是“JAN”而不是“JAN”。 DateTimeFormatter没有意识到这一点。并将模式更改为 “d-MMM-yyyy” 来源:首先。大写的
D
表示一年中的天字段(而不是您想要的月日),大写的Y
表示基于周的年字段(而不是您想要的年)。正确的模式是小写字母d
和y
此外,您使用的月份名称是大写字母(JAN
和FEB
),因此格式化程序必须不区分大小写(JAN
和FEB
)。这些月份名称是英文缩写,因此您还必须使用英语语言环境来确保正确解析名称(使用java.util.locale
class)
因此,您的格式化程序应按如下方式创建:
DateTimeFormatter df = new DateTimeFormatterBuilder()
// case insensitive to parse JAN and FEB
.parseCaseInsensitive()
// add pattern
.appendPattern("dd-MMM-yyyy")
// create formatter (use English Locale to parse month names)
.toFormatter(Locale.ENGLISH);
这将使您的代码正常工作(并且
datediff
将是32
)。也许您可以使用此通配符
String d2arr[] = {
"2016-12-21",
"1/17/2016",
"1/3/2016",
"11/23/2016",
"OCT 20 2016",
"Oct 22 2016",
"Oct 23", // default year is 2016
"OCT 24", // default year is 2016
};
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.parseCaseInsensitive().parseLenient()
.parseDefaulting(ChronoField.YEAR_OF_ERA, 2016L)
.appendPattern("[yyyy-MM-dd]")
.appendPattern("[M/dd/yyyy]")
.appendPattern("[M/d/yyyy]")
.appendPattern("[MM/dd/yyyy]")
.appendPattern("[MMM dd yyyy]");
DateTimeFormatter formatter2 = builder.toFormatter(Locale.ENGLISH);
尝试使用
DateTimeFormatter dtf=DateTimeFormatter.of模式(“dd LLL yyyy”,Locale.ENGLISH)代码>尝试使用“dd-MMM-yyyy”它不起作用..可能重复感谢。它起作用了。我还有一个问题。@Uma说:“它也能保证日光节约时间吗?日光节约时间不会有任何问题。”。即使DST在3月26日开始,从3月25日到3月26日还有整整一天。使用LocalDate
时,完全忽略了实际只有23小时。事实上,LocalDate
忽略了任何时区,因此完全忽略了任何DST。回答得非常好。涵盖了所有相关内容,并切中要害。添加区域设置非常关键,从我的错误消息中看不明显(DateTimeParseException:无法在索引0处分析文本)。谢谢你的提示!我确实试过这个,但它对我不起作用。它只通过使用其他解决方案中提到的ParseCaseSensitive()起作用。
String d2arr[] = {
"2016-12-21",
"1/17/2016",
"1/3/2016",
"11/23/2016",
"OCT 20 2016",
"Oct 22 2016",
"Oct 23", // default year is 2016
"OCT 24", // default year is 2016
};
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.parseCaseInsensitive().parseLenient()
.parseDefaulting(ChronoField.YEAR_OF_ERA, 2016L)
.appendPattern("[yyyy-MM-dd]")
.appendPattern("[M/dd/yyyy]")
.appendPattern("[M/d/yyyy]")
.appendPattern("[MM/dd/yyyy]")
.appendPattern("[MMM dd yyyy]");
DateTimeFormatter formatter2 = builder.toFormatter(Locale.ENGLISH);
// DateTimeFormatterBuilder provides custom way to create a
// formatter
// It is Case Insensitive, Nov , nov and NOV will be treated same
DateTimeFormatter f = new DateTimeFormatterBuilder().parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("yyyy-MMM-dd")).toFormatter();
try {
LocalDate datetime = LocalDate.parse("2019-DeC-22", f);
System.out.println(datetime); // 2019-12-22
} catch (DateTimeParseException e) {
// Exception handling message/mechanism/logging as per company standard
}