【发布时间】:2020-10-21 02:25:05
【问题描述】:
我有一个日期字符串如下
String test Date = "1/31/2020";
我正在使用下面的代码
public static String getPeriodMonth(String periodEndDate) {
LocalDate localDate;
YearMonth yearMonth = null;
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
localDate = LocalDate.parse(periodEndDate, formatter);
yearMonth = YearMonth.from(localDate);
}catch (Exception e) {
LOGGER.error("Error: "+ e.getMessage() + ">>" + e.getCause());
}
return yearMonth.toString();
}
执行此代码时出现以下异常:
Error: Text '1/31/2020' could not be parsed at index 0>>null
有人可以帮助我在这里做错了什么吗?
【问题讨论】:
-
顺便说一句,看起来您可以直接解析为
YearMonth,而无需先解析为LocalDate:yearMonth = YearMonth.parse(periodEndDate, formatter);
标签: java date java-time date-parsing