gpt4 book ai didi

java - 使用俄罗斯语言环境时 Java 中的 TimeZone 显示问题

转载 作者:行者123 更新时间:2023-12-04 06:48:33 26 4
gpt4 key购买 nike

我在使用俄语语言环境时遇到问题,日期的时区部分没有转换为俄语。
即如果“雅库茨克时间 2010 年 8 月 10 日下午 4:02:09”是时间,
它正在转换为 -
使用俄罗斯语言环境 - "10.08.2010 16:02:09 Yakutsk Time 10"
使用法语区域设置 - "août 2010 16:02:09 Heure du Iakoutsk"

我正在使用以下代码(我的服务器支持俄语语言环境)

     SimpleDateFormat formatterWithoutTimezone = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");

SimpleDateFormat formatterServerTimezone = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss zzz");
TimeZone serverTimezone = TimeZone.getDefault();
formatterServerTimezone.setTimeZone(serverTimezone);
String dateSrcStr = formatterWithoutTimezone.format(dateSrc) + " UTC";
Date dateServerTimezone = formatterServerTimezone.parse(dateSrcStr);
DateFormat displayFormatter = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
String formatedDate = displayFormatter.format(dateServerTimezone) + " "
+ serverTimezone.getDisplayName(locale);

最佳答案

时区名称由 Java 从 sun.util.resources.TimeZoneNamesBundle 获得。有一个 TimeZoneNames 基础资源类,并且有本地化(参见 rt.jar):

TimeZoneNames_de.class  
TimeZoneNames_en.class
TimeZoneNames_en_CA.class
TimeZoneNames_en_GB.class
TimeZoneNames_en_IE.class
TimeZoneNames_es.class
TimeZoneNames_fr.class
TimeZoneNames_it.class
TimeZoneNames_sv.class

所以只有上述语言有本地化的时区名称。以下测试确认大多数语言环境没有本地化时区名称:
TimeZone timeZone = TimeZone.getTimeZone("Asia/Yakutsk");
for (Locale locale : Locale.getAvailableLocales()) {
System.out.println(locale.getDisplayName() + timeZone.getDisplayName(locale));
}

关于java - 使用俄罗斯语言环境时 Java 中的 TimeZone 显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3446730/

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