[英]LocalDate.parse can't parse the character on the last index wehere there is no character (nor empty space)
我的記事本文件中有以下文本:
19-12-2021
我檢查了是否有任何空格,沒有。
當我寫:
String noticeLastUpdateDate = textFileDAO.getMoneyTableNoticeLastUpdateDate();
DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate dateNow = LocalDate.parse(noticeLastUpdateDate, formatterDate);
我收到一個錯誤:
java.time.format.DateTimeParseException: Text '19-12-2021
' could not be parsed, unparsed text found at index 10
但是沒有索引10? 為什么它認為有索引[10]?
在使用String::trim
解析日期之前修剪可能的空格:
String trimmedDate = textFileDAO.getMoneyTableNoticeLastUpdateDate().trim();
DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate dateNow = LocalDate.parse(trimmedDate, formatterDate);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.