gpt4 book ai didi

java - 为什么我在 Java 8 中无法正确解析这个英文日期?

转载 作者:行者123 更新时间:2023-12-02 06:02:58 24 4
gpt4 key购买 nike

我想执行一个简单的示例来用模式解析字符串。

String input = "Sep 31 2013";
LocalDate localDate = LocalDate.parse(input,
DateTimeFormatter.ofPattern("MMM d yyyy"));

它抛出异常:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sep 31 2013' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDate.parse(Unknown Source)
at lambda.DateTime.main(DateTime.java:78)

我使用 java 8 中的 java.time 包。

最佳答案

我假设您有一个非英语的区域设置。如果您想用英语解析,请使用适当的Locale

String input = "Sep 31 2013";
LocalDate localDate = LocalDate.parse(input, DateTimeFormatter
.ofPattern("MMM d yyyy").withLocale(Locale.ENGLISH));

或任何其他英语区域设置:美国、加拿大、英国等。

或者,对于您的区域设置ru_RU,传递俄语日期字符串,即。其中 Sep 是俄语,以便可以正确解析

关于java - 为什么我在 Java 8 中无法正确解析这个英文日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22970229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com