如何使用 Google 时区 API 获取 GMT
How to Get GMT by using Google Time Zone API
我想检索印度的 GMT(5.5) 或其他国家/地区的 GMT(5.5)。下面是我使用 Google 时区 API 的代码,即将到来的响应是关于 dstoffset、rawOffset、status、timeZoneId、timeZoneName。我的问题是如何获得 GMT (5.5) 谢谢
final String registerURL = "https://maps.googleapis.com/maps/api/timezone/json?location=30.293461,78.524094×tamp=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);
rawOffset
is the offset from UTC (in seconds) for the given location.
由于UTC和GMT没有时差,rawOffset
除以3600就可以得到你请求的时区的GMT时间。
显然,当除以 3600 时,答案是小时的分数,而不是 hour:min
对。所以,如果你得到输出,比方说,4.50
,那实际上是 4.5 hours
,它与 4:30 hours (4 hours 30 minutes)
.
相同
我想检索印度的 GMT(5.5) 或其他国家/地区的 GMT(5.5)。下面是我使用 Google 时区 API 的代码,即将到来的响应是关于 dstoffset、rawOffset、status、timeZoneId、timeZoneName。我的问题是如何获得 GMT (5.5) 谢谢
final String registerURL = "https://maps.googleapis.com/maps/api/timezone/json?location=30.293461,78.524094×tamp=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);
rawOffset
is the offset from UTC (in seconds) for the given location.
由于UTC和GMT没有时差,rawOffset
除以3600就可以得到你请求的时区的GMT时间。
显然,当除以 3600 时,答案是小时的分数,而不是 hour:min
对。所以,如果你得到输出,比方说,4.50
,那实际上是 4.5 hours
,它与 4:30 hours (4 hours 30 minutes)
.