从YearMonth(yyyy-MMMM)到YearMonth(yyyy-MMMM) [英] From YearMonth(yyyy-MM) to YearMonth(yyyy-MMMM)

查看:101
本文介绍了从YearMonth(yyyy-MMMM)到YearMonth(yyyy-MMMM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量:

YearMonth date;

其中内立 2016-07 例如。

我希望它仍然是YearMonth,但使用 2016年7月 (请注意,没有-分隔符),或者甚至更好的是 2016 luglio ,即意大利语Locale。

I want it to be still YearMonth, but with "2016 july"(note , there's no "-" separator), or, even better, "2016 luglio", that is italian Locale.

怎么做?

UPDATE

UPDATE

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

I tried with JackDaniels's method. It could actually work, as it gives me a string(str) with the right format of date. But I need to put that string again into my YearMonth variable. I tried with:

myVariable = YearMonth.parse(str);

但它返回一个错误: java.time.format.DateTimeParseException:文本'2014 gennaio'无法在索引4处解析

我该如何解决?

推荐答案

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

Use the new DateTimeFormatter to parse and format YearMonth directly and use Locale to set the locale you need. Don't use SimpleDateFormat. That is part of the old Date API.

尝试运行代码以查看是否正是您想要的 Codiva在线编译器IDE 。 / p>

Try running the code to see if this is what you want in 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));

科迪瓦

这篇关于从YearMonth(yyyy-MMMM)到YearMonth(yyyy-MMMM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆