如何从Android中的Locale值获取时区?

时间:2012-01-12 05:05:12

标签: java android timezone locale

您好我想从语言环境中获取时区。我的代码是

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


}
}

但它不起作用。如果我将Locale值转换为String值意味着它是否有效? 以及如何转换这个?有可能获得时区吗?任何人都可以告诉我。提前谢谢。

2 个答案:

答案 0 :(得分:3)

大多数应用程序将使用getDefault()根据程序运行的时区返回TimeZone。

您还可以按ID获取特定的TimeZone。

有关详细信息,请参阅here

答案 1 :(得分:0)

不要这样做。从用户自己获取时区。请他们设置时区偏移量。英语加拿大有一个地区,但有六个时区!美国大陆是一个地区,但至少有4个5个时区。