2014-12-11 40 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

相關問題