显式使用 withLocale
将区域设置设置为德语会导致 am/pm 字符串在时间值中本地化,但不会转换为适合该区域设置的 24 小时时间格式。
当创建新进程时,完成Locale.setLocale(Locale.GERMAN);
后,格式是正确的。
withLocale()
不应该影响相关区域设置的所有方面吗?
// Code snippet where call is being made:
Log.d(TAG, "XYZZY getDefault(): " + Locale.getDefault().toString());
DateTimeFormatter timeFormat = DateTimeFormat.shortTime()
.withLocale(Locale.getDefault());
Log.d(TAG, "XYZZY timeFormat.locale: " +
timeFormat.getLocale().toString());
dateString = alarmTime.toString(timeFormat);
Log.d(TAG, "XYZZY dateString: "+ dateString);
当变量 alarmTime
的值为 11:00pm(2300 小时)时:
10-04 14:17:41.492 (23609): XYZZY getDefault(): en_US
10-04 14:17:41.493 (23609): XYZZY timeFormat.locale: en_US
10-04 14:17:41.495 (23609): XYZZY dateString: 11:00 PM
现在切换到德语语言环境并重新执行相同的代码,注意 “PM”的字符串更改为德语,但时间格式不变(后缀 应抑制,时间值应为 23:00):
10-04 14:18:15.066 (23609): XYZZY getDefault(): de_DE
10-04 14:18:15.066 (23609): XYZZY timeFormat.locale: de_DE
10-04 14:18:15.067 (23609): XYZZY dateString: 11:00 nachm.
等待进程消失并重新启动,将区域设置保留为 德语,现在返回德语的正确时间格式:
10-04 14:18:54.497 (23881): XYZZY getDefault(): de_DE
10-04 14:18:54.497 (23881): XYZZY timeFormat.locale: de_DE
10-04 14:18:54.498 (23881): XYZZY dateString: 23:00
最佳答案
免责声明:不是答案,因为我无法重现,但作为答案编写是为了显示我如何尝试重现问题的详细信息。
我正在时区 America/New_York
中以区域设置 en_US
运行,使用 JDK 1.8.0_91 和 Joda-Time 2.9.9 .
测试
LocalTime time = LocalTime.parse("23:00");
System.out.println(time);
System.out.println(time.toString(DateTimeFormat.shortTime()));
System.out.println(time.toString(DateTimeFormat.shortTime().withLocale(Locale.GERMANY)));
输出
23:00:00.000
11:00 PM
23:00
shortTime()
工作正常,无论是否更改语言环境。
JVM 默认区域设置从未更改。
关于java - 使用 Joda DateTimeFormatter.withLocale() 仅影响 am/pm 的字符串值,而不影响时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46572492/