2014-12-11 70 views
1

我在时区显示中遇到问题。我需要显示“12/11/2014 11:45 IST”的时区和时间。我可以展示时间。但是我不能显示哪个区域是PST, ESTIST。我该怎么做?任何人都可以帮助我?如何在android中获取某个地点的时区?

我的源代码是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); 
+2

请尝试https://developers.google.com/maps/documentation/timezone/ – jenuine 2014-12-11 06:14:25

回答

1
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像您期望的时区。

0

你有没有使用TimeZone.getDefault(): 大多数应用程序将使用TimeZone.getDefault()返回基于在程序运行时区时区。

欲了解更多信息:http://developer.android.com/reference/java/util/TimeZone.html

试试下面的代码也:

TimeZone tz = TimeZone.getDefault(); 
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " tz.getID()); 
+0

它给出的结果为:TimeZone GMT + 05:30 Timezon id :: Asia/Calcutta。 我需要得到IST而不是格林威治标准时间 – Parthi 2014-12-11 06:32:56

1

您可以使用SimpleDateFormat格式化你的DateTimeZone以简单的方式。例如,要显示日期TimeZone12/11/2014 11:45 IST,您可以使用dd/MM/yyyy HH:mm z格式,其中z将表示TimeZone,如EST, IST

Calendar cal = Calendar.getInstance(); 
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm z"); 
String formatedDate = dateFormat.format(cal.getTime()); 
+0

我得到了和11/12/2014 12:07 GMT + 05:30相同的结果。我需要得到IST或PST而不是GMT。 – Parthi 2014-12-11 06:39:41

1

我解决了通过以下步骤:

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

相关问题