Search code examples
javaandroidgoogle-mapsgoogle-maps-timezone

How to Get GMT by using Google Time Zone API


I want to retrieve GMT(5.5) of India or same like this for other Country. Below is my code where I used Google Time Zone API and the coming response is on dstoffset, rawOffset, status, timeZoneId, timeZoneName. My question is how to get GMT (5.5) Thank you

 final String registerURL = "https://maps.googleapis.com/maps/api/timezone/json?location=30.293461,78.524094&timestamp=1331161200&key=API_KEY";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, registerURL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);
                        String s1 = obj.getString("dstOffset");
                        String s2 = obj.getString("rawOffset");
                        String s3 = obj.getString("status");
                        String s4 = obj.getString("timeZoneId");
                        String s5 = obj.getString("timeZoneName");
                        TimeZone tz1 = TimeZone.getTimeZone(s4);
                        TimeZone tz2 = TimeZone.getTimeZone(s4);
                        tvTimeZone.setText(String.valueOf(tz2));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                        @Override
                        // Handles errors that occur due to Volley
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "Error");
                        }
                    }
            );

            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
            requestQueue.add(stringRequest);

Solution

  • rawOffset is the offset from UTC (in seconds) for the given location.

    As UTC and GMT has no time difference between them, dividing rawOffset by 3600 you can get the GMT time of your requested time zone.

    And obviously, when divided by 3600, the answer is in fractions of hour not in the hour:min pair. So, if you get the output, say, 4.50, that is actually 4.5 hours which is the same as 4:30 hours (4 hours 30 minutes).