如何获取android中某个地方的时区?

时间:2014-12-11 06:11:56

标签: android datetime calendar timezone

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

5 个答案:

答案 0 :(得分:1)

您可以使用SimpleDateFormatDate格式化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());

答案 1 :(得分: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 :(得分: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。

答案 3 :(得分:0)

您是否使用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());

答案 4 :(得分:0)

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