Default Android Time zone List

纵饮孤独 提交于 2019-12-23 08:27:17

问题


I was wondering if i can get somewhere the default code for time zone selection list for android version 2.3.3?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/15542063/default-android-time-zone-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!