DateTimeParseException因未知原因而抛出

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

DateTimeParseException thrown for unknown reason

问题

这是完整的错误行:

java.time.format.DateTimeParseException: 无法解析文本 '01-Jan-2020',位于索引 0 处

这是导致错误的代码段:

val DATETIME_FORMAT = DateTimeFormatter.ofPattern("d-MMM-yyyy").withZone(ZoneId.of("UTC"))
val parsedTime = DATETIME_FORMAT.parse(input)

input 变量的值为 '01-Jan-2020',如错误行所示。
我已经查看了许多类似的问题,但这不是预期的解决方案。有谁可以告诉我如何解决这个问题吗?

英文:

Here is the full error line:

java.time.format.DateTimeParseexception: Text '01-Jan-2020' could not be parsed at index 0

Here is the code I am using that throws the error:

val DATETIME_FORMAT = DateTimeFormatter.ofPattern("d-MMM-yyyy").withZone(ZoneId.of("UTC"))
val parsedTime = DATETIME_FORMAT.parse(input)

the input variable is '01-Jan-2020' as seen in the error line.
I have looked at many similar questions to this one but it's not the expected solution. Can anybody enlighten me on how to solve this problem?

答案1

得分: 1

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH).withZone(ZoneId.of("UTC"));
var parsedTime = dateTimeFormatter.parse("01-Jan-2020");
英文:

You have to set Locale as second parameter ofPattern method, look below (working code in java)

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH).withZone(ZoneId.of("UTC"));

var parsedTime = dateTimeFormatter.parse("01-Jan-2020");

huangapple
  • 本文由 发表于 2020年10月27日 21:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64555834.html
匿名

发表评论

匿名网友

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

确定