Hi I want to get currency name by GMT Time Zone. I got Time Zone and corresponding name of the time zone. The code is
TimeZone tz = TimeZone.getDefault();
String gmt1=TimeZone.getTimeZone(tz.getID()).getDisplayName(false,TimeZone.SHORT);
String gmt2=TimeZone.get开发者_运维问答TimeZone(tz.getID()).getDisplayName(false,TimeZone.LONG);
Log.d("Tag","TimeZone : "+gmt1+"\t"+gmt2);
Now I want to get currency name like if that time zone is Indian standard Time means that will be show the currency is Rupee.
Currency current=Currency.getInstance(gmt1);
String current1=current.toString();
System.out.println(current1);
I tried by this code but i can't get it. Anybody tell me what is the mistake on my code and how to do? Thanks in advance.
There is no reliable mapping of time zones or time zone names to currencies. Your current approach won't work in a lot of countries.
The issue with your above code is that the Currency
objects takes either a 3-letter currency name as a string or a Locale object. In order to get this information you really need to know the country. Unfortunately a timezone does not give you specific enough information about the locale.
There are, after all a limited number of unique time zones, and by far more countries. For example, look at all the countries in Africa that are on a variant of UTC+1 (Wikipedia GMT article)
You'll need to come up with a country code in order to accomplish this.
Currency.getInstance
takes an ISO 4217 currency codes as parameter, not a time zone name. Many countries can share the same time zone, you can't tell the local currency by the time zone someone is in.
Maybe you should use a locale as parameter (probably the user's default locale).
精彩评论