我有一个变量:
YearMonth date;例如,站在里面的"2016-07"。
我希望它仍然是YearMonth,但是对于"2016 july"(注意,没有"-“分隔符),或者,更好的是,"2016 luglio",这是意大利地区。
该怎么做呢?
更新
我试过杰克丹尼尔斯的方法。它实际上可以工作,因为它为我提供了一个具有正确日期格式的字符串(str)。但是,我需要将该字符串再次放入我的YearMonth变量中。我试过:
myVariable = YearMonth.parse(str);但是它返回了一个错误:java.time.format.DateTimeParseException: Text '2014 gennaio' could not be parsed at index 4
我该怎么解决这个问题?
发布于 2016-07-11 08:31:25
使用新的DateTimeFormatter直接解析和格式化YearMonth,并使用Locale设置所需的区域设置。不要使用SimpleDateFormat。这是旧日期API.的一部分。
试着运行代码,看看这是否是您在Codiva联机编译器IDE中想要的。
YearMonth yearMonth = YearMonth.of(2016, 7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMMM",
new Locale("it", "IT"));
System.out.println(yearMonth.format(formatter));科迪娃中的完整工作代码。
https://stackoverflow.com/questions/38301807
复制相似问题