2013-06-27 215 views
3

在Java中,我使用語言和國家參數 創建語言環境,然後使用適當的日期格式。Java默認語言環境回退

但是Java不支持所有的Language_Country組合和回退到默認語言環境格式(在我的情況下是en_US)。

有沒有方法可以確定發生故障?

E.g:

當我創建Locale locale = new Locale("xx","xx");

然後

((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT,locale)).toLocalizedPattern();

回報M/d/yy所以回退爲en_US語言環境。 。

所以,我需要這樣的東西

locale.isDefault

+0

有你試過'Locale.setDefault()'? – fge

回答

1

您可以檢查該區域是the available locales之一:

Set<Locale> locales = new HashSet<>(); 
Collections.addAll(locales, Locale.getAvailableLocales()); 

Locale locale = new Locale("xx", "xx"); 
System.out.println(locales.contains(locale)); //prints false 

locale = new Locale("fr", "FR"); 
System.out.println(locales.contains(locale)); //prints true 
+0

謝謝,它似乎是我在找:) –

0

Locale.getDefault()等於(區域)

+0

不幸的是,它返回錯誤的語言環境xx_XX –

相關問題