Products

 

LAYOUT_FRAGMENT:right.template
 
PlexGlobeLib
This library is used to manipulate world-wide calendars, dates, and time-zones. It comes with over tweleve thousands of world-wide cities with their latitude/longitude, time-zones, day-light-savings and other information. It can be used to convert dates from one time-zone to another or from one calendar to another calendar.

Java Docs

Java Docs API

Price

Email support@plexobject.com to find prices.

Download

You need JRE 1.4 or newer to use this software. The latest version of JRE can be download from http://java.sun.com/j2se/

After unzipping it, please read the README file and LICENSE file. Once, you agree to the LICENSE file, you can start using the library. The library contains all the jar files needed in WEB-INF/lib directory. It also comes with a tag library that you can use in your JSP code. There are two types of examples provided in this library, the first example is in Example.java that shows how to invoke basic APIs. The second example uses JSP index.jsp file and shows JSP tags. The programming APIs are defined in the docs directory.

License Agreement:


Usage


/**
 * @author: PlexObject Solutions, Inc.
 *
 * <B>CLASS COMMENTS</B>
 * Class Name: Example
 * Class Description:
 *  This class shows example of operations defined in IGlobeFacade
 * Known Bugs:
 *
 * Invariants:
 *
 */
 

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.GregorianCalendar;
import com.ibm.icu.util.TimeZone;
import com.plexobject.iploc.GlobeFacade;
import com.plexobject.iploc.IGlobeFacade;
import com.plexobject.iploc.Location;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.util.Date;
import java.util.Random;
import java.util.Vector;
import java.util.StringTokenizer;



public class Example {
  public static final String[][] CITIES_COUNTRIES = new String[][] {
    new String[] {"Aalborg", "Denmark"},
    new String[] {"Abu Dhabi", "United Arab Emirates"},
    new String[] {"Agra", "India", "AGR"},
    new String[] {"Amman", "Jordan"},
    new String[] {"Ankara", "Turkey"},
    new String[] {"Athens", "Greece"},
    new String[] {"Bangalore", "India"},
    new String[] {"Barcelona", "Spain"},
    new String[] {"Beijing", "China"},
    new String[] {"Berlin", "Germany"},
    new String[] {"Bombay", "India"},
    new String[] {"Brussels", "Belgium"},
    new String[] {"Budapest", "Hungary"},
    new String[] {"Cairo", "Egypt"},
    new String[] {"Chicago", "IL,USA"},
    new String[] {"Damascus", "Syria"},
    new String[] {"Denver", "CO,USA"},
    new String[] {"Durban", "South Africa"},
    new String[] {"Toronto", "Canada"},
    new String[] {"London", "England"},
    new String[] {"Los Angeles", "CA,USA"},
    new String[] {"Melbourne", "Australia"},
    new String[] {"Moruya", "Australia"},
    new String[] {"Moscow", "Russia", "VKO"},
    new String[] {"New York", "NY,USA"},
    new String[] {"Oslo", "Norway"},
    new String[] {"Paris", "France"},
    new String[] {"Pescara", "Italy"},
    new String[] {"Prague", "Czechoslovakia"},
    new String[] {"Rome", "Italy"},
    new String[] {"Zurich", "Switzerland"}
  };


  private IGlobeFacade facade;
  private Location[] cities;
  public Example() throws IOException {
    facade = new GlobeFacade();
    cities = new Location[CITIES_COUNTRIES.length];
    for (int i=0; i<CITIES_COUNTRIES.length; i++) {
      String city = CITIES_COUNTRIES[i][0];
      String country = CITIES_COUNTRIES[i][1];
      String state = null;
      // we will match state for US cities
      if (country.indexOf("USA") != -1) {
    int n = country.indexOf(",");
    if (n != -1) {
      state = country.substring(0, n).trim();
      country = country.substring(n+1).trim();
    }
      }
      String countryCode = facade.getCountryCode(country);
      if (countryCode == null) {
        System.err.println("*******Country code for " + country + 
    " is not found");
    continue;
      }
      Location[] locs = facade.getCities(countryCode);
      //System.out.println("Found " + locs.length + " cities for " + country);
      String[] ids = facade.getTimeZoneIDs(countryCode);
      //System.out.println("Found " + ids.length + " time-zones for " +country);

      cities[i] = facade.findLocationByCity(city, state, countryCode); 
      if (cities[i] == null) System.err.println("*******Unable to find city " 
    + city + " in " + country);
    } // for (int i=0; i<CITIES_COUNTRIES.length; i++) 
  }

  // randomly picked cities
  public void test() throws IOException {
    Location src = getRandomCity(); 
    Location dst = getRandomCity(); 
    test(src, dst);
    test(src, facade.getLocalTimeZoneLocation());
    test(facade.getLocalTimeZoneLocation(), dst);
  }

  // user entered cities
  public void testCity(String city, String country) throws IOException {
    test(findCity(city, country));
  }
  public void testZone(String srcZone, String dstZone) throws IOException {
    test(facade.toZoneLocation(srcZone), facade.toZoneLocation(dstZone));
  }
  public void testZone(String zone) throws IOException {
    test(facade.toZoneLocation(zone));
  }


  public Location findCity(String city, String country) throws IOException {
    String countryCode = facade.getCountryCode(country);
    if (countryCode == null) {
      System.err.println("Country code for " + country + " is not found");
      return null;
    }
/*
    String[] ids = facade.getTimeZoneIDs(countryCode);
    System.out.println("TimeZones for " + country + ": " + ids.length);
    for (int i=0; i<ids.length; i++) {
      System.out.println("\t\t" + ids[i]);
    }
*/
    String lcity = city.toLowerCase();
    Location[] locs = facade.getCities(countryCode);
    if (locs == null) {
      System.err.println("Cities for " + country + " are not found");
      return null;
    }
    //System.out.println("Found " + locs.length + " cities");
    for (int i=0; i<locs.length; i++) {
      if (locs[i].hasCity() && locs[i].getCity().equalsIgnoreCase(city)) 
    return locs[i];
    }
    for (int i=locs.length-1; i>=0; i--) {
      if (locs[i].hasCity() && locs[i].getCity().toLowerCase().indexOf(lcity) 
    != -1) return locs[i];
    }
    return null;
  }

  public void test(Location src, Location dst) throws IOException {
    if (src == null) {
      System.out.println("No source location");
      return;
    }
    if (dst == null) {
      System.out.println("No target location");
      return;
    }
    Date srcDate = new Date();
    String dstDate = facade.findTimeAtTimeZone(src, dst, srcDate, null);
    DateFormat sdf = src.getDateFormat();
    System.out.println("TIME\n\t" + src.getCityStateCountry() + ":" + 
    src.getTimeZone() + ":" + sdf.format(srcDate) + "\n==>\n\t" + 
    dst.getCityStateCountry() + ":" + dstDate);
    double diff = facade.calculateTimeZoneDifference(src, dst); 
    System.out.println("DIFFERENCE " + diff);
  }


  public void test(Location loc) throws IOException {
    if (loc == null) {
      System.out.println("No location");
      return;
    }
    String dstDate = facade.currentTimeAtTimeZone(loc, null);
    System.out.println(loc.getCityStateCountry() + ":" + loc.getTimeZone() 
    + "=" + dstDate);
  }

  /**
   * getTimeZone - instantiates time-zone for the given time-zone-id
   * @param id time-zone-id
   * @return time-zone
   */
  public com.ibm.icu.util.TimeZone getTimeZone(String id) {
    return TimeZone.getTimeZone(id);
  }

  protected Location getRandomCity() {
    do {
      int index = nextNumber(0, cities.length-1);
      if (cities[index] != null) return cities[index];
    } while (true);
  }

  /**
   * nextNumber randomly picks a number between min and max inclusive 
   * @param min 
   * @param max
   * @return random number
   */
  public static int nextNumber(int min, int max) {
    Random random = new Random();
    long seed = Math.abs((long) (System.currentTimeMillis() * Math.random()));
    random.setSeed(seed);
    int n = Math.abs(Math.abs(random.nextInt()) % (max-min+1));
    return n + min;
  }

  public com.ibm.icu.util.GregorianCalendar getCalendar() throws IOException {
    return facade.newGregorianCalendar();
  } 
  public static String[] split(String str) {
    if (str == null) return null;
    return split(str, null);
  }
  public static String[] split(String str, String delim) {
    if (str == null) return null;
    str = str.trim();
    if (str.length() == 0) return new String[0];
    Vector vec = new Vector();
    StringTokenizer st = null;
    if (delim == null) st = new StringTokenizer(str);
    else st = new StringTokenizer(str, delim);
    while (st.hasMoreTokens()) {
      vec.addElement(st.nextToken());
    }
    String[] tokens = new String[vec.size()];
    vec.copyInto(tokens);
    for (int i=0; i<tokens.length; i++) tokens[i] = trimWhitespace(tokens[i]);
    return tokens;
  }
  public static String trimWhitespace(String s) {
    if(s == null) {
      return(s);
    }
    return(trimRightWS(trimLeftWS(s)));
  }

  public static String trimLeftWS(String s) {
    if(s == null) {
      return(s);
    }
    for(int i=0; i<s.length(); i++) {
      if(!Character.isWhitespace(s.charAt(i))) {
        return(s.substring(i));
      }
    }
    return(s);
  }

  public static String trimRightWS(String s) {
    if(s == null) {
      return(s);
    }

    for(int i=s.length()-1; i>=0; i--) {
      if(!Character.isWhitespace(s.charAt(i))) {
        return(s.substring(0,i+1));
      }
    }

    return(s);
  }

  public static String trimWS(String s) {
    return(trimWhitespace(s));
  }

  public static void main(String[] args) throws Exception {
    long started = System.currentTimeMillis();
    Example ex = new Example();
    long elapsed = System.currentTimeMillis() - started;
    System.out.println("Initialized in " + elapsed + " millis");
    if (args.length == 2) {
      //ex.testCity(args[0], args[1]);
      ex.testZone(args[0], args[1]);
    } else {
      System.out.println("Enter \"time-zone\" or \"city name, country\"");
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String line;
      while ((line=in.readLine()) != null) {
        String[] words = split(line, ",");
        if (words.length == 1) ex.testZone(line);
        else if (words.length == 2) ex.testCity(words[0], words[1]);
      }
    }
    System.exit(0);
  }
}