malavika
malavika

Reputation: 1331

How to get get Time Zone from Locale value in android?

Hi I want to get Time Zone from locale. My code is

    package com.my.country;

    import java.text.DateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Locale;
    import java.util.Map;
    import java.util.StringTokenizer;
    import java.util.TimeZone;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.TextView;

    public class StoreCountry extends Activity 
   {
String countryname="";
Spinner spinnerAvailableID,availableCurrencycode;
TextView textTimeZone,countrycodetxt;
ArrayAdapter<String> timeAdapter,codeAdapter;

   @Override

   public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.select);

   String selectString = getString(R.string.select);

   spinnerAvailableID = (Spinner)findViewById(R.id.availableID);
   availableCurrencycode = (Spinner)findViewById(R.id.availablecurrency);

   textTimeZone = (TextView)findViewById(R.id.timezone);
  countrycodetxt = (TextView)findViewById(R.id.countrycode);


   ArrayList<String> timeArray = new ArrayList<String>();
   timeArray.add(selectString);
   String[] idArray = TimeZone.getAvailableIDs();
   for(int i=0;i<idArray.length;i++)
   {
       String mycon=idArray[i];
       timeArray.add(mycon);
   }
   System.out.println(timeArray);
   timeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, timeArray);
   timeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   spinnerAvailableID.setAdapter(timeAdapter);
   spinnerAvailableID.setOnItemSelectedListener(new OnItemSelectedListener()
   {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,int position, long id) 
        {
            String selectedId = (String)(parent.getItemAtPosition(position));
            System.out.println(selectedId);
            TimeZone tz = TimeZone.getTimeZone(selectedId);
            String timezone=TimeZone.getTimeZone(tz.getID()).getDisplayName(false,TimeZone.SHORT);
            String timezonename=TimeZone.getTimeZone(tz.getID()).getDisplayName(false,TimeZone.LONG);
            Log.d("Tag","TimeZone : "+timezone+"\t"+timezonename);
            textTimeZone.setText(timezone+"\t"+timezonename);

            DateFormat df = DateFormat.getTimeInstance();
            df.setTimeZone(TimeZone.getTimeZone(timezone));
            String gmtTime = df.format(new Date());
            System.out.println(gmtTime);

            StringTokenizer st2 = new StringTokenizer(selectedId,"/");  
            while(st2.hasMoreTokens())
            {
                countryname= st2.nextToken();
                System.out.println(countryname);
                break;
            }


        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }});


   ArrayList<String> codeArray=new ArrayList<String>();
   codeArray.add(selectString);
   CurrencySymbol cs = new CurrencySymbol();
   Map<String, String> currencies = cs.getAvailableCurrencies();
   for (String country : currencies.keySet()) 
   {      
       String currencyCode = currencies.get(country);
       codeArray.add(currencyCode);
       System.out.println(country + " => " + currencyCode);
   }       
   codeAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, codeArray);
   codeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   availableCurrencycode.setAdapter(codeAdapter);
   availableCurrencycode.setOnItemSelectedListener(new OnItemSelectedListener()
   {
    @Override
    public void onItemSelected(AdapterView<?> parent1, View view1, int position1,long id1) 
    {
        String selectedCode = (String)(parent1.getItemAtPosition(position1));
        System.out.println(selectedCode);
        countrycodetxt.setText(selectedCode);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) 
    {       

    }     });

   /*ArrayList<String> countryArray = new ArrayList<String>();
   timeArray.add(selectString);*/


   Locale[] myCountry = Locale.getAvailableLocales();
   for(int i=0;i<myCountry.length;i++)
   {
       Locale mycon=myCountry[i];

       String name1=mycon.getDisplayName();
       //timeArray.add(mycon);
      // String name=mycon.getDisplayCountry(mycon);

       System.out.println("locale name--->"+mycon);
       //System.out.println("country name--->"+name);
       System.out.println("country name =--->"+name1);

       TimeZone time = TimeZone.getTimeZone(name1);
        String timezone=TimeZone.getTimeZone(time.getID()).getDisplayName(false,TimeZone.SHORT);
        System.out.println("time zone"+timezone);
      // String time=mytime.getDisplayName();
     //  System.out.println("time zone---->"+mytime);
   }


}
}

But it doesn't work. If i convert Locale value into String value means is it work? And how to convert this? And is it possible to get Time Zone? Can anybody tell me. Thanks in advance.

Upvotes: 3

Views: 10633

Answers (2)

Bill Rosmus
Bill Rosmus

Reputation: 3011

Don't do this. Get the time zone from the user themselves. Ask them to set a timezone offset. English Canada has one locale but six timezones! The mainland United States is one locale, but has at least 4 maybe 5 timezones.

Upvotes: 0

himanshu
himanshu

Reputation: 1980

Most applications will use getDefault() which returns a TimeZone based on the time zone where the program is running.

You can also get a specific TimeZone by id.

See here for more information.

Upvotes: 3

Related Questions