In case you need it:
Code Select
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;
import java.util.Vector;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.stefan1200.jts3serverquery.JTS3ServerQuery;
public class JTS3TimeZone {
private boolean DEBUG;
JTS3ServerQuery queryLib;
HashMap<String, String> clientInfo;
HashMap<Integer, TimeZone> clientZone;
// If you don't have an API_KEY yet, you can get one for free by registering at http://www.ipinfodb.com/register.php.
private final String apiKey = ;
public JTS3TimeZone(
JTS3ServerQuery queryLib) {
clientZone = new HashMap<Integer, TimeZone>();
this.queryLib = queryLib;
// DEBUG = true;
}
public TimeZone getTimeZone(Integer clientID) {
TimeZone result;
if (clientZone.containsKey(clientID)) {
result = clientZone.get(clientID);
} else {
result = getTimeZone(getIpAddress(clientID));
clientZone.put(clientID, result);
}
return getTimeZone(getIpAddress(clientID));
}
public TimeZone getTimeZone(String ipAddress) {
TimeZone result = null;
URL localURL;
try {
localURL = new URL("http://api.ipinfodb.com/v3/ip-city/?format=json&key=" + apiKey + "&ip=" + ipAddress);
URLConnection localURLConnection = localURL.openConnection();
localURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
localURLConnection.setRequestProperty("Accept-Language", "es-es,es;q=0.8,en-us;q=0.5,en;q=0.3");
localURLConnection.setRequestProperty("Accept-Encoding", "paco");
localURLConnection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
localURLConnection.setRequestProperty("Connection", "close");
localURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
localURLConnection.setConnectTimeout(10000);
BufferedReader clanBufferedReader = null;
StringBuilder data = new StringBuilder(3000);
try {
clanBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream(), "UTF8"));
for (String line; (line = clanBufferedReader.readLine()) != null; data.append(line.replace(" ", "").replace("\t", "")))
;
} finally {
if (clanBufferedReader != null)
clanBufferedReader.close();
}
if (DEBUG)
System.out.println(data);
JsonParser jsonParser = new JsonParser();
JsonObject json = jsonParser.parse(data.toString()).getAsJsonObject();
if (!"ok".equalsIgnoreCase(json.get("statusCode").getAsString())) {
throw new Exception("Error in API status: " + json.get("statusMessage").getAsString());
}
result = TimeZone.getTimeZone("GMT" + json.get("timeZone").getAsString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private String getIpAddress(int clientID) {
return queryLib.getInfo(JTS3ServerQuery.INFOMODE_CLIENTINFO, clientID).get("connection_client_ip");
}
}