Custom date format in android for given locale

时光总嘲笑我的痴心妄想 提交于 2019-12-06 15:48:24

I think the problem is that Macedonia is not a supported locale on the Android JVM. If you run your code as plain Java console app, it's fine. The method Locale.getAvailableLocales() returns 152 members in plain Java, only 88 in an Android emulator. If you have the code snippet:

Locale[] locales = Locale.getAvailableLocales();
   String cCode;
   for (Locale loc :locales){
       cCode = loc.getCountry();
       if (cCode.equalsIgnoreCase("MK"))
            Toast.makeText(this, cCode, Toast.LENGTH_SHORT).show();
        // Or System.out.println() in a Java app    
}

Then the toast doesn't show for "MK" although it will println in the Java app

From documentation of SimpleDateFormat:

**Text**: For formatting, if the number of pattern letters is 4 or more, 
the full form is used; otherwise a short or abbreviated form is used if 
available. For parsing, both forms are accepted, independent of the 
number of pattern letters.

So this should fix it:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK"));

NickT was faster :-), so just adding to his answer: if you want to see your locales supported on Android, run:

for (Locale l:Locale.getAvailableLocales()) {
    Log.d(l.getDisplayCountry(),l.toString()); 
}

and you will see that Macedonia is not on the list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!