我有一个变量:
YearMonth date;
里面站立的地方"2016-07"
, 例如。
我希望它仍然是 YearMonth,但是"2016 july"
(注意,没有“-”分隔符),或者更好的是,"2016 luglio"
,即意大利语 Locale。
怎么做?
UPDATE
我尝试了杰克丹尼尔斯的方法。它实际上可以工作,因为它给了我一个字符串(str
)具有正确的日期格式。但我需要再次将该字符串放入我的 YearMonth 变量中。我尝试过:
myVariable = YearMonth.parse(str);
但它返回给我一个错误:java.time.format.DateTimeParseException: Text '2014 gennaio' could not be parsed at index 4
我该如何解决这个问题?
使用新的日期时间格式化程序直接解析和格式化 YearMonth 并使用 Locale 设置您需要的区域设置。不要使用 SimpleDateFormat。这是旧 Date API 的一部分。
尝试运行代码看看这是否是你想要的.
YearMonth yearMonth = YearMonth.of(2016, 7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMMM",
new Locale("it", "IT"));
System.out.println(yearMonth.format(formatter));
完整的工作代码在.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)