英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论