我在时区显示方面遇到问题。我需要显示时区和时间,例如"12/11/2014 11:45 IST"。我可以显示时间。但我无法显示哪个地方是PST, EST
或IST
的区域。我该怎么做?有人可以帮助我吗?
我的源代码很糟糕。
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+"×tamp="+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;
}
结果将为您提供预期的时区。
你是否使用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());
SimpleDateFormat 以简单的方式使用 TimeZone
格式化Date
。例如,要将日期显示为12/11/2014 11:45 IST
TimeZone
,您可以使用如下所示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());
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;
}
下面是一个示例,说明如何确定应用程序的时间与系统时区位于不同的时区。如果是 - 打开时间设置
TimeZone.setDefault(null) // clear cached timezone
val localeCountry = Locale.getDefault().country // get default country
code
val availableTimeZoneList = TimeZone.getAvailableIDs(localeCountry).toList() // available time zone list for default country code
val currentTimeZone = TimeZone.getDefault()
if(!availableTimeZoneList.contains(currentTimeZone.id) && availableTimeZoneList.all { TimeZone.getTimeZone(it).rawOffset != currentTimeZone.rawOffset }) {
startActivity(Intent(android.provider.Settings.ACTION_DATE_SETTINGS))
}