The method getAvailableIDs() returns the system's installed time zone IDs. Any of these IDs can be passed to getTimeZone() to lookup the corresponding time zone instance.
package com.exercise.AndroidTimeZone;
import java.util.TimeZone;
import android.app.Activity;
import android.os.Bundle;
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 AndroidTimeZoneActivity extends Activity {
Spinner spinnerAvailableID;
TextView textTimeZone;
ArrayAdapter<String> idAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerAvailableID = (Spinner)findViewById(R.id.availableID);
textTimeZone = (TextView)findViewById(R.id.timezone);
String[] idArray = TimeZone.getAvailableIDs();
idAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, idArray);
idAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAvailableID.setAdapter(idAdapter);
spinnerAvailableID.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String selectedId = (String)(parent.getItemAtPosition(position));
TimeZone timezone = TimeZone.getTimeZone(selectedId);
String TimeZoneName = timezone.getDisplayName();
int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
textTimeZone.setText(TimeZoneName + " : " +String.valueOf(TimeZoneOffset));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/availableID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/timezone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Related Post:
- Get time zone where currently running, using TimeZone.getDefault()
No comments:
Post a Comment