gpt4 book ai didi

java - LocalDate 和 DateTimeFormatter 无法在日语语言环境中正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:11 25 4
gpt4 key购买 nike

我在测试我的某些方法时遇到了一个奇怪的问题,我似乎已经能够获得我的问题的具体示例。

我正在使用 ja_JP_JP_#u-ca-japanese 语言环境,无法使用语言环境定义的自己的日期模式来解析日期。

我想知道我是否做错了什么,或者这是一个 JDK 错误。

请注意,为了构建ja_JP_JP_#u-ca-japanese,您需要使用new Locale("ja", "JP", "JP") 根据摘自 the Locale javadoc :

Special cases

For compatibility reasons, two non-conforming locales are treated as special cases. These are ja_JP_JP and th_TH_TH. These are ill-formed in BCP 47 since the variants are too short. To ease migration to BCP 47, these are treated specially during construction. These two cases (and only these) cause a constructor to generate an extension, all other values behave exactly as they did prior to Java 7.

Java has used ja_JP_JP to represent Japanese as used in Japan together with the Japanese Imperial calendar. This is now representable using a Unicode locale extension, by specifying the Unicode locale key ca (for "calendar") and type japanese. When the Locale constructor is called with the arguments "ja", "JP", "JP", the extension "u-ca-japanese" is automatically added.

Java has used th_TH_TH to represent Thai as used in Thailand together with Thai digits. This is also now representable using a Unicode locale extension, by specifying the Unicode locale key nu (for "number") and value thai. When the Locale constructor is called with the arguments "th", "TH", "TH", the extension "u-nu-thai" is automatically added.

演示问题的给定测试用例:

@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);
}

此代码适用于任何其他正常语言环境,例如英语等。

最佳答案

往返能力确实有效,但它需要所有数据都可用。

在第一种情况下,您需要使用 withLocale()withChronology() 方法指定语言环境 年表:

LocalDate date = LocalDate.of(2014, 10, 2);
Locale jpLocale = new Locale("ja", "JP", "JP");
Chronology chrono = Chronology.ofLocale(jpLocale);
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(jpLocale)
.withChronology(chrono);
String jpDateStr = date.format(f);
LocalDate result = LocalDate.parse(jpDateStr, f);

第二种情况也是如此,使用 ofPattern() 锁定语言环境,但不锁定时间顺序:

String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT, null, chrono, jpLocale);
DateTimeFormatter f = DateTimeFormatter.ofPattern(pattern, jpLocale)
.withChronology(chrono);
LocalDate jpLocalDate = LocalDate.parse(jpDateStr, f);

只有当语言环境和时间顺序都可用并被使用时,往返才有可能。您的问题是基于尝试使用日语语言环境进行格式化,但不一致地应用日语年表。

关于java - LocalDate 和 DateTimeFormatter 无法在日语语言环境中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26169008/

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