Java Data Type How to - Convert timezone name to city name (remove continent part)








Question

We would like to know how to convert timezone name to city name (remove continent part).

Answer

import java.util.TimeZone;
// w  w  w.  ja v a  2 s  .  com
public class Main {

  public static void main(String[] args) {
    String[] ids = TimeZone.getAvailableIDs();

    for (String id : ids) {
      int slash = id.indexOf('/');
      String stripped = id.substring(slash + 1).replace("_", " ");
      System.out.println(stripped);
    }
  }
}