I ran into some strange problem while testing my method, and it seems I was able to get a concrete example of my problem.
I use the ja_JP_JP_#u-ca-japanese locale, unable to parse the date using its own date pattern defined in the locale.
I am wondering if I am doing something wrong, or if this is a JDK error.
Note that to build ja_JP_JP_#u-ca-japanese you need to use new Locale("ja", "JP", "JP") according to the excerpt from Locale javadoc :
Special occasions
For compatibility reasons, two inappropriate locales are considered special cases. These are ja_JP_JP and th_TH_TH. They are poorly formed in BCP 47 because the options are too short. To facilitate the transition to BCP 47, they are specially treated during construction. These two cases (and only they) force the constructor to generate the extension, all other values behave exactly the same as before Java 7.
Java used ja_JP_JP to represent the Japanese used in Japan along with the Japanese imperial calendar. This can now be represented using the Unicode locale extension by specifying the Unicode ca locale key (for "calendar") and enter japanese. When the Locale constructor is called with the arguments "ja", "JP", "JP", the extension "u-ca-japanese" is automatically added.
Java uses th_TH_TH to represent Thai in Thailand along with Thai numbers. This can also now be represented using the Unicode locale extension, specifying the Unicode locale key nu (for "number") and the value thai. When the Locale constructor is called with arguments "th", "TH", "TH", the extension "u-nu-thai" is automatically added.
This test case demonstrates the problem:
@Test public void testJapaneseLocale() { LocalDate specificLocalDate = LocalDate.of(2014, 10, 2); Locale jpLocale = new Locale("ja", "JP", "JP"); DateTimeFormatter jpDateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(jpLocale); String jpDate = specificLocalDate.format(jpDateTimeFormatter); String jpPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null, Chronology.ofLocale(jpLocale), jpLocale); LocalDate jpLocalDate = LocalDate.parse(jpDate, DateTimeFormatter.ofPattern(jpPattern, jpLocale)); assertEquals(specificLocalDate, jpLocalDate); }
This code works for any other normal language such as English, etc.