【问题标题】:How to get the time zone of a place in android?如何在android中获取一个地方的时区?
【发布时间】:2015-02-09 13:26:14
【问题描述】:

我的时区显示有问题。我需要显示时区和时间,例如“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);

【问题讨论】:

标签: java android datetime calendar timezone


【解决方案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。

【讨论】:

    【解决方案2】:

    你有没有使用 TimeZone.getDefault(): 大多数应用程序将使用 TimeZone.getDefault(),它根据程序运行的时区返回 TimeZone。

    欲了解更多信息: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());
    

    【讨论】:

    • 它给出的结果为:TimeZone GMT+05:30 Timezon id :: Asia/Calcutta。我需要获取 IST 而不是 GMT
    【解决方案3】:

    您可以使用SimpleDateFormat 以简单的方式将Date 格式化为TimeZone。例如,要将TimeZone 的日期显示为12/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());
    

    【讨论】:

    • 我得到了与 2014 年 11 月 12 日 12:07 GMT+05:30 相同的结果。我需要获取 IST 或 PST 而不是 GMT。
    【解决方案4】:
    public static String getPSTDate() {
            String returnFormat = "";
            try {
                Date startTime = new Date();
                TimeZone pstTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
                DateFormat formatter = DateFormat.getDateInstance();
                formatter.setTimeZone(pstTimeZone);
                returnFormat = formatter.format(startTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return returnFormat;
        }
    
        public static String getPSTime() {
            String returnFormat = "";
            try {
                Date startTime = new Date();
                TimeZone pstTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
                DateFormat formatter = DateFormat.getTimeInstance();
                formatter.setTimeZone(pstTimeZone);
                returnFormat = formatter.format(startTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return returnFormat;
        }
    

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2020-10-26
      • 2015-06-12
      • 1970-01-01
      • 2011-10-09
      相关资源
      最近更新 更多