gpt4 book ai didi

java - 如何在android中获取一个地方的时区?

转载 作者:行者123 更新时间:2023-11-29 17:44:48 27 4
gpt4 key购买 nike

我在显示时区时遇到问题。我需要显示时区和时间,如“12/11/2014 11:45 IST”。我可以显示时间。但我无法显示区域是 PST、ESTIST。我该怎么做?谁能帮帮我?

我的源代码是错误的。

    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);

最佳答案

 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;

}

结果将按照您期望的时区为您提供 IST。

关于java - 如何在android中获取一个地方的时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27416464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com