Reputation: 195
I was wondering if i can get somewhere the default code for time zone selection list for android version 2.3.3?
Upvotes: 8
Views: 10874
Reputation: 4009
Unfortunately, Android settings takes that timezone list from an XML included in the Settings application. You can see this in the source code (line 161):
private List<HashMap> getZones() {
List<HashMap> myData = new ArrayList<HashMap>();
long date = Calendar.getInstance().getTimeInMillis();
try {
XmlResourceParser xrp = getResources().getXml(R.xml.timezones);
...
}
Other applications, such as Google Calendar, have their own arrays of values. It can be checked by doing reverse engineering of those resources.
So, unless you want to maintain your own list (taking multilanguage into consideration...), I'd recommend you to have a reduced list of that provided by java.util.TimeZone and show only those timezones you want to, as @Raghunandan recommended you in his answer's comment.
Upvotes: 7
Reputation: 133560
String[] ids=TimeZone.getAvailableIDs();
for(int i=0;i<ids.length;i++)
{
System.out.println("Availalbe ids.................."+ids[i]);
TimeZone d= TimeZone.getTimeZone(ids[i]);
System.out.println("time zone."+d.getDisplayName());
System.out.println("savings."+d.getDSTSavings());
System.out.println("offset."+d.getRawOffset());
/////////////////////////////////////////////////////
if (!ids[i].matches(".*/.*")) {
continue;
}
String region = ids[i].replaceAll(".*/", "").replaceAll("_", " ");
int hours = Math.abs(d.getRawOffset()) / 3600000;
int minutes = Math.abs(d.getRawOffset() / 60000) % 60;
String sign = d.getRawOffset() >= 0 ? "+" : "-";
String timeZonePretty = String.format("(UTC %s %02d:%02d) %s", sign, hours, minutes, region);
System.out.println(timeZonePretty);
//////////////////////////////////////////////////////////////////
}
ListView listitems=(ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ids);
listitems.setAdapter(adapter);
}
The documentation for TimeZone.http://developer.android.com/reference/java/util/TimeZone.html
To set time zone
Following is sample code for settings Time Zone of according to America
// First Create Object of Calendar Class
Calendar calendar = Calendar.getInstance();
// Now Set the Date using DateFormat Class
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
// Finally Set the time zone using SimpleDateFormat Class's setTimeZone() Method
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
Upvotes: 11