java - TimeZone customized display name -


i need timezone display values follows :

(utc + 05:30) chennai, kolkata, mumbai, new delhi 

but using following method getting bit different output. how should timezone display name above ? (if required, can use joda).

public class timezoneutil {      private static final string timezone_id_prefixes =             "^(africa|america|asia|atlantic|australia|europe|indian|pacific)/.*";     private static list<timezone> timezones;      public static list<timezone> gettimezones() {         if (timezones == null) {             timezones = new arraylist<timezone>();             final string[] timezoneids = timezone.getavailableids();             (final string id : timezoneids) {                 if (id.matches(timezone_id_prefixes)) {                     timezones.add(timezone.gettimezone(id));                 }             }              collections.sort(timezones, new comparator<timezone>() {                  public int compare(final timezone t1, final timezone t2) {                     return t1.getid().compareto(t2.getid());                 }             });         }          return timezones;     }      public static string getname(timezone timezone) {         return timezone.getid().replaceall("_", " ") + " - " + timezone.getdisplayname();     }      public static void main(string[] args) {         timezones = gettimezones();         (timezone timezone : timezones) {             system.out.println(getname(timezone));         }     } } 

this code may trick you:

public static void main(string[] args) {      (string timezoneid: timezone.getavailableids()) {         timezone timezone = timezone.gettimezone(timezoneid);          // filter out timezone ids such "gmt+3"; more thorough filtering required though         if (!timezoneid.matches(".*/.*")) {             continue;         }          string region = timezoneid.replaceall(".*/", "").replaceall("_", " ");         int hours = math.abs(timezone.getrawoffset()) / 3600000;         int minutes = math.abs(timezone.getrawoffset() / 60000) % 60;         string sign = timezone.getrawoffset() >= 0 ? "+" : "-";          string timezonepretty = string.format("(utc %s %02d:%02d) %s", sign, hours, minutes, region);         system.out.println(timezonepretty);     } } 

the output looks this:

(utc + 09:00) tokyo

there are, however, few caveats:

  • i filter out timezones id matches format "continent/region" (e.g. "america/new_york"). have more thorough filtering process rid of outputs such (utc - 08:00) gmt+8 though.

  • you should read documentation timezone.getrawoffset() , understand it's doing. example, doesn't dst effects consideration.

  • on whole, should know messy approach, because timezone id can of many different formats. maybe restrict down timezones matter application, , have key value mapping of timezone ids display names?


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -