Get country name from country code

2019-03-22 19:15发布

问题:

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!

回答1:

try like this

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

Hope this will help out.



回答2:

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.



回答3:

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



回答4:

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);
}