解析从 Web 服务检索到的 UTC 日期为本地日期
Parsing UTC date retrieved from web service results in local date
我的目标是简单地从 Web 服务中检索当前的 UTC 时间并生成一个日期供使用。我遇到的麻烦是,每当我使用我的 SimpleDateFormatter 来解析日期字符串时,我都会以当地时间结束。我花了一段时间研究解决方案,但没有任何效果。这是我的代码并检查附加图像的调试值。感谢任何人的帮助。
URL url = new URL("http://www.timeapi.org/utc/now");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String timeStamp = response.toString(); // response is in this format 2015-09-19T01:09:35+01:00
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(timeStamp);
return date;
java.util.Date
对象中没有任何时区信息。
Date 对象包含的唯一内容是自 "epoch" - 1970 年 1 月 1 日 00:00:00 UTC.
以来的毫秒数
您的字符串已包含时区 +01:00
,由于 yyyy-MM-dd'T'HH:mm:ssZ
中的 Z
,SimpleDateFormat
已成功使用它。所以你的 date
对象是 你需要的.
在调试期间(您的屏幕截图),您会看到本地时间的日期。这是 Date.toString()
:
的源代码
public String toString() {
// TODO: equivalent to the following one-liner, though that's slower on stingray
// at 476us versus 69us...
// return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(d, Locale.US);
LocaleData localeData = LocaleData.get(Locale.US);
TimeZone tz = TimeZone.getDefault();
Calendar cal = new GregorianCalendar(tz, Locale.US);
cal.setTimeInMillis(milliseconds);
StringBuilder result = new StringBuilder();
result.append(localeData.shortWeekdayNames[cal.get(Calendar.DAY_OF_WEEK)]);
result.append(' ');
result.append(localeData.shortMonthNames[cal.get(Calendar.MONTH)]);
result.append(' ');
appendTwoDigits(result, cal.get(Calendar.DAY_OF_MONTH));
result.append(' ');
appendTwoDigits(result, cal.get(Calendar.HOUR_OF_DAY));
result.append(':');
appendTwoDigits(result, cal.get(Calendar.MINUTE));
result.append(':');
appendTwoDigits(result, cal.get(Calendar.SECOND));
result.append(' ');
result.append(tz.getDisplayName(tz.inDaylightTime(this), TimeZone.SHORT, Locale.US));
result.append(' ');
result.append(cal.get(Calendar.YEAR));
return result.toString();
}
下一行
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
在您需要以 UTC (DateFormat.format(Date date)
) 获取日期的字符串表示时有效。
我的目标是简单地从 Web 服务中检索当前的 UTC 时间并生成一个日期供使用。我遇到的麻烦是,每当我使用我的 SimpleDateFormatter 来解析日期字符串时,我都会以当地时间结束。我花了一段时间研究解决方案,但没有任何效果。这是我的代码并检查附加图像的调试值。感谢任何人的帮助。
URL url = new URL("http://www.timeapi.org/utc/now");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String timeStamp = response.toString(); // response is in this format 2015-09-19T01:09:35+01:00
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(timeStamp);
return date;
java.util.Date
对象中没有任何时区信息。
Date 对象包含的唯一内容是自 "epoch" - 1970 年 1 月 1 日 00:00:00 UTC.
您的字符串已包含时区 +01:00
,由于 yyyy-MM-dd'T'HH:mm:ssZ
中的 Z
,SimpleDateFormat
已成功使用它。所以你的 date
对象是 你需要的.
在调试期间(您的屏幕截图),您会看到本地时间的日期。这是 Date.toString()
:
public String toString() {
// TODO: equivalent to the following one-liner, though that's slower on stingray
// at 476us versus 69us...
// return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(d, Locale.US);
LocaleData localeData = LocaleData.get(Locale.US);
TimeZone tz = TimeZone.getDefault();
Calendar cal = new GregorianCalendar(tz, Locale.US);
cal.setTimeInMillis(milliseconds);
StringBuilder result = new StringBuilder();
result.append(localeData.shortWeekdayNames[cal.get(Calendar.DAY_OF_WEEK)]);
result.append(' ');
result.append(localeData.shortMonthNames[cal.get(Calendar.MONTH)]);
result.append(' ');
appendTwoDigits(result, cal.get(Calendar.DAY_OF_MONTH));
result.append(' ');
appendTwoDigits(result, cal.get(Calendar.HOUR_OF_DAY));
result.append(':');
appendTwoDigits(result, cal.get(Calendar.MINUTE));
result.append(':');
appendTwoDigits(result, cal.get(Calendar.SECOND));
result.append(' ');
result.append(tz.getDisplayName(tz.inDaylightTime(this), TimeZone.SHORT, Locale.US));
result.append(' ');
result.append(cal.get(Calendar.YEAR));
return result.toString();
}
下一行
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
在您需要以 UTC (DateFormat.format(Date date)
) 获取日期的字符串表示时有效。