Search code examples
javaandroiddatetimecalendartimezone

How to get the time zone of a place in android?


I have a problem in timezone showing. I need to show the time zone and time like "12/11/2014 11:45 IST". I can show the time. But I can't show the zone which place is PST, EST, or IST. How can I do it? Anyone can help me?

My source code is blelow.

    Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

Solution

  •  public static String TimezoneUrl = "https://maps.googleapis.com/maps/api/timezone/json?";
    
     API_KEY="Your API service key";
     newurl = TimezoneUrl+"location="+myLatitude+","
     +myLongitude+"&timestamp="+System.currentTimeMillis() / 1000 + "&key=" + API_KEY;
     response = makeServiceCall(url, ServiceHandler.GET);
    
     jsonResponse = new JSONObject(response);
     timesone = jsonResponse.getString("timeZoneName");
    
    
     for (int i = 0; i < timesone.length(); i++) {
            if (Character.isUpperCase(timesone.charAt(i))) {
                char c = timesone.charAt(i);
                timezone = timezone + c;
        }
     }
    
      public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
     }
    
    
     public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
     try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
    
        //httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Custom user agent");
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
    
        // Checking http request method type
         if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils    .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);
    
            httpResponse = httpClient.execute(httpGet);
    
        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
    
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    return response;
    
    }
    

    Result will give you IST as you expect Timezone.