我从网络中获取了一个日期/时间字符串,格式为 "yyyy/mm/dd'T'HH:MM:SS'Z'",它是以UTC时区表示的。
现在我需要确定设备当前所处的时区,然后将此时间转换为本地时间。
我该怎么做?
(FYI,目前UTC时间是上午10:25,在印度当前时间是下午3:55)
TimeZone.getDefault()
代替TimeZone.getTimeZone("GMT")
。 // note that I modified the format string slightly
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'");
// set the timezone to the original date string's timezone
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = fmt.parse("1998/12/21T13:29:31Z", new ParsePosition(0));
// then reset to the target date string's (local) timezone
fmt.setTimeZone(TimeZone.getDefault());
String localTime = fmt.format(date);
或者,使用两个单独的SimpleDateFormat实例,一个用于原始时间,另一个用于目标时间。