'How to get Time Zone through IP Address in JAVA

I want to get time zone through IP Address in JAVA.

Actually i have an application which will run at the client machine.

I have IP address of client machine. But not able to get the time zone for each client machine.

In client machine



Solution 1:[1]

For getting timezone through IP Address; there are GeoTimezone where there are certain range of IP that represent a perticuler TIMEZONE and country. You can download that all file that is in CSV and create a database. Then through sql query you can get your timezone.

Solution 2:[2]

In Java will give you timezone

java.util.TimeZone.getDefault();

If you want to get client's time zone at server, then call this at client and send back to server.

Solution 3:[3]

I don't think that it is possible. But what can you do is to get the time zone using JavaScript through HTML and then retrieve this data through form submission or something like that.

Solution 4:[4]

Use getZoneFromIpAddress(String ip) :

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
        }
        return sb.toString();
    }
    
        public String getZoneFormIpAddress(String ip) throws IOException{
          String zone = "inconnu";
          try {
              JSONObject json = readJsonFromUrl("https://api.ipdata.co/"+ip+"?api-key=test");
              zone = json.get("country_name")+" "+json.get("city");
          } catch (Exception e) {
          }
          return zone;
    } 
      
        public String getJsonCodeFromUrl(String url) throws IOException{
          JSONObject json = readJsonFromUrl(url);
          return json.toString();}
          public String getJsonValueByKey(String url, String key) throws IOException{
          JSONObject json = readJsonFromUrl(url);
          return (String) json.get(key);}


    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {

        InputStream is = new URL(url).openStream();

        try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
          String jsonText = readAll(rd);
          JSONObject json = new JSONObject(jsonText);
          return json;
        } finally {
          is.close();
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kumar
Solution 2 Nikolay Kuznetsov
Solution 3 Amber
Solution 4