gpt4 book ai didi

java - Java日期解析代码有什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:11 25 4
gpt4 key购买 nike

这段 Java 代码:

LocalDate.parse("12 Сен 2018", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(new Locale("ru", "RUS")).ofPattern("dd MMM yyyy"));

导致

java.time.format.DateTimeParseException: Text '12 Сен 2018' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)

尝试了所有 Proper Russian month string translation Java示例 - 没有结果。

这段代码有什么问题?

注意:我使用的是 Java 8

更新:在本地机器和在线编译器上试过这个 - 在线编译器在本地机器上工作 - 出现错误。

    Map<Long, String> map = new HashMap<>();
map.put(1L, "Янв");
map.put(2L, "Фев");
map.put(3L, "Мар");
map.put(4L, "Апр");
map.put(5L, "Май");
map.put(6L, "Июн");
map.put(7L, "Июл");
map.put(8L, "Авг");
map.put(9L, "Cен");
map.put(10L, "Окт");
map.put(11L, "Ноя");
map.put(12L, "Дек");
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("dd ")
.appendText(ChronoField.MONTH_OF_YEAR, map)
.appendPattern(" yyyy")
.toFormatter(new Locale("ru", "RU"));
System.out.println(LocalDate.parse("12 Cен 2018", formatter));

最佳答案

好吧,你需要为此创建一个自定义格式化程序,这并不复杂(第一次了解俄语,回答一个 stackoverflow 问题很方便):

Map<Long, String> map = new HashMap<>();
map.put(1L, "янв");
map.put(2L, "фев");
map.put(3L, "мар");
map.put(4L, "апр");
map.put(5L, "май");
map.put(6L, "июн");
map.put(7L, "июл");
map.put(8L, "авг");
map.put(9L, "сен");
map.put(10L, "окт");
map.put(11L, "ноя");
map.put(12L, "дек");
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("dd ")
.appendText(ChronoField.MONTH_OF_YEAR, map)
.appendPattern(" yyyy")
.toFormatter(new Locale("ru"));

System.out.println(LocalDate.parse("12 сен 2018", fmt)); // 2018-09-12

关于java - Java日期解析代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52308766/

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