- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在测试我的某些方法时遇到了一个奇怪的问题,我似乎已经能够获得我的问题的具体示例。
我正在使用 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/
我正在尝试在日本网站上的 flash 动态文本字段上实现抗锯齿。这些文本字段的内容不是固定数据。 我没有嵌入字体的选项,因为嵌入所有字符会导致非常重的 swf 文件(3000kb+ 仅用于字体)。这是
我正在创建一个游戏。我有一些带有文本的用户界面。最近我们想添加日语版本,但我遇到字体问题。我使用 stb_freetype 来光栅化字体,并且我支持 Unicode,所以这应该不是问题。但大多数字体似
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我知道我的问题已经在这里有了解决方案。但我只想具体说明我的情况。我有一个 json 对象,其中包含非英语字符。 例如。 {“my_chinise_name”:“吉米”}。 该对象将通过 javascr
我刚刚安装了 Tesseract OCR 并在运行命令 $ tesseract --list-langs 后输出仅显示 2 种语言,eng和osd 。我的问题是,如何加载另一种语言,在我的例子中是日语
在哪里可以获得与日语汉字、平假名和片假名字符对应的 ASCII 代码列表。我正在做一个java函数和Javascript来确定它是否是日语字符。它的 ASCII 码范围是多少? 最佳答案 ASCII代
在我的 Rails 应用程序中,我有一个像这样的文本字段 @disabled_field %> 我正在使用 jQuery 来验证此文本字段仅接受 half width katakana字符(Unic
我有在表单中添加网站选项的功能。 用户可以在这里写域/url,这个域/url 可以是英语也可以是日语,如下所示。 www.google.com www.南极星.com I am using follo
我遇到了日语问题。我有一个允许用户搜索数据的表格。当用户输入要搜索的字符串并按下“Enter”键时,搜索功能就会执行。我的代码是: $('#formSearch input').keyup(funct
我是 Java 脚本的新手。我写了一个正则表达式,它允许用户名或电子邮件地址作为某些条件的输入,例如应接受 6-50 个字符不允许空格或空白不应允许 2 个 @ 符号并且可以接受超过 1 个 .(点)
您好,我有一个包含日语文本的文件,保存为 unicode 文件。 我需要从文件中读取信息并将信息显示到标准输出。 我正在使用 Visual Studio 2008 int main() {
我正在使用在 http://mobile.tutsplus.com/tutorials/android/android-sdk-using-the-text-to-speech-engine/ 中找到
(我正在开发一个网站来抓取 ASP.NET 中的其他网站内容。我能够正确获取内容,但如何根据该内容识别使用的是哪种语言。例如英语、印地语、中文, 日语等 我使用了以下代码。 HttpWebReques
在 TextBox 中,我无法捕获具有特殊 IME 的语言(例如日语和韩语)的击键,因为 IME 不断地决定不同的字符或音节。实际上,大多数情况下 KeyUp/KeyDown 都不会触发(如果 IME
我有一个包含日语单词的数据库。我很困惑,因为这个查询: SELECT japanese FROM my_table where japanese = 'する' 返回两个结果: ずる する 我试图查看
我想将 CSV 文件解析为 JSON 文件。我已经解析了它,但它没有得到日语字符。 我正在使用 Papa Parser 将 CSV 解析为 JSON。 这是我的代码:- Papa.parse("htt
我有一个 C# winform 应用程序,它安装在日文 windows 7 上。一些标签以非常宽的字体显示,导致它们无法匹配 from 的大小。 经过一些研究,我被告知这可能是半角/全角问题。有没有办
MySql 全文搜索是否适用于非拉丁语言? (希伯来语、阿拉伯语、日语……) 添加:做了一些测试...希伯来语有一些问题。示例:名称 מוסəנזון 的发音与 מושəנזון 相同,但搜索不会找到
我使用下一个代码从句柄为 hFile 的文件中读取所有元素行得通,而且我用 GetFileSize(hFile, NULL) 得到了它的大小. _TCHAR* text = (_TCHAR*)mall
验证按预期进行,但消息仅以英语显示良好。我使用自己的日语消息。 问题是我收到了日语的乱码验证消息。我将validation-api-1.0.0.GA.jar与hibernate-validator-4
我是一名优秀的程序员,十分优秀!