英文:
java.time.format.DateTimeParseException for dd-MMM-yy format
问题
我已经尝试了几个小时来将这个字符串解析为LocalDate,但我就是找不到我的模式哪里有问题。
public void parseDate() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd'-'MMM'-'yy");
LocalDate date = LocalDate.parse("05-Sep-20", formatter);
}
我得到了以下异常:
java.time.format.DateTimeParseException: Text '05-Sep-20' could not be parsed at index 3
当使用模式 dd'-'MMMM'-'yy
或 dd'-'MM'-'yy
时,它可以正常工作,但对于 MMM
就不行。
我在破折号周围使用了单引号,否则我会在不同的索引处得到解析异常。
我正在使用Java 1.8和java.time
。
英文:
I am trying for hours now to parse this String to LocalDate and I just can't find where my pattern is wrong.
public void parseDate() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd'-'MMM'-'yy");
LocalDate date = LocalDate.parse("05-Sep-20", formatter);
}
I get the following exception:
java.time.format.DateTimeParseException: Text '05-Sep-20' could not be parsed at index 3
It works fine when using the pattern dd'-'MMMM'-'yy
or dd'-'MM'-'yy
, but it just won't work for MMM
I used the single quotes around the dash, because otherwise I was getting a parsing exception at a different index.
I am using Java1.8 and java.time
答案1
得分: 0
这可能与您的区域设置有关。请尝试以下操作:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd'-'MMM'-'yy", Locale.US);
LocalDate date = LocalDate.parse("05-Sep-20", formatter);
参考链接:https://stackoverflow.com/a/44928297
英文:
It could be your locale. Try this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd'-'MMM'-'yy", Locale.US);
LocalDate date = LocalDate.parse("05-Sep-20", formatter);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论