gpt4 book ai didi

java - 从年月 (yyyy-MM) 到年月 (yyyy-MMMM)

转载 作者:行者123 更新时间:2023-12-02 03:29:26 27 4
gpt4 key购买 nike

我有一个变量:

YearMonth date;

里面站在哪里"2016-07" ,例如。

我希望它仍然是 YearMonth,但带有 "2016 july" (注意,没有“-”分隔符),或者更好的是 "2016 luglio" ,即意大利语 Locale。

怎么做呢?

更新

我尝试了 JackDaniels 的方法。它实际上可以工作,因为它给了我一个具有正确日期格式的字符串( str )。但我需要再次将该字符串放入我的 YearMonth 变量中。我尝试过:

myVariable = YearMonth.parse(str);

但它返回一个错误:java.time.format.DateTimeParseException: Text '2014 gennaio' could not be parsed at index 4

如何解决这个问题?

最佳答案

使用新的DateTimeFormatter直接解析和格式化YearMonth,并使用Locale设置您需要的区域设置。不要使用 SimpleDateFormat。这是旧 Date API 的一部分。

尝试运行代码看看这是否是您想要的 Codiva online compiler IDE 中的内容.

YearMonth yearMonth = YearMonth.of(2016, 7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMMM",
new Locale("it", "IT"));
System.out.println(yearMonth.format(formatter));

完整的工作代码在 Codiva .

关于java - 从年月 (yyyy-MM) 到年月 (yyyy-MMMM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301807/

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