2012-01-12 3 views
3

Salut, je veux obtenir le fuseau horaire des paramètres régionaux. Mon code estComment obtenir le fuseau horaire de valeur Locale dans Android?

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


} 
} 

Mais cela ne fonctionne pas. Si je convertis la valeur de Locale en valeur de chaîne, est-ce que cela fonctionne? Et comment convertir cela? Et est-il possible d'obtenir Time Zone? Quelqu'un peut-il me le dire. Merci d'avance.

Répondre

3

La plupart des applications utiliseront getDefault() qui renvoie un fuseau horaire en fonction du fuseau horaire dans lequel le programme s'exécute.

Vous pouvez également obtenir un fuseau horaire spécifique par ID. Pour plus d'informations, voir here pour plus d'informations.

0

Ne faites pas cela. Obtenez le fuseau horaire de l'utilisateur eux-mêmes. Demandez-leur de définir un décalage de fuseau horaire. Le Canada anglais a un lieu mais six fuseaux horaires! La partie continentale des États-Unis est un lieu, mais a au moins 4, voire 5 fuseaux horaires.

Questions connexes