java.time.format.DateTimeParseException for dd-MMM-yy format

huangapple go评论76阅读模式
英文:

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'-'yydd'-'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);

ref: https://stackoverflow.com/a/44928297

huangapple
  • 本文由 发表于 2020年9月11日 07:04:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63838733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定