Get country name from country code

2019-03-22 18:33发布

I'd need to get the full country name from the country code. For example for Netherlands, I'd need the Netherlands from the country code NL.

I thought I could do that with Locale like:

Locale loc = new Locale("NL");
loc.getCountry();

but loc.getCountry(); is empty.

Any idea about how to do this, please? Thanks in advance!

4条回答
萌系小妹纸
2楼-- · 2019-03-22 19:07

This should work:

Locale l = new Locale("", "NL");
String country = l.getDisplayCountry();

The first parameter of Locale is the language, which is not useful in your case.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-22 19:20

Try to use the other constructor

Locale loc = new Locale("NL", "The Netherlands");

Locale There does not appear to be a predefined Locale for The Netherlands

查看更多
Anthone
4楼-- · 2019-03-22 19:25

try like this

Locale loc = new Locale("","NL");
loc.getDisplayCountry();

Hope this will help out.

查看更多
再贱就再见
5楼-- · 2019-03-22 19:25

for a full solution TelephonyManager (from this solution):

TelephonyManager teleMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String localeCountry = teleMgr.getNetworkCountryIso();
if (localeCountry != null) {
    Locale loc = new Locale("",localeCountry);
    Log.d(TAG, "User is from " + loc);
}
查看更多
登录 后发表回答