根据当前时区将UTC日期/时间显示为日期/时间

Par*_*ani 7 java datetime android

我从网上获得了一个日期/时间字符串,格式为"yyyy/mm/dd'T'HH:MM:SS'Z'",它是UTC格式.

现在我必须确定设备的当前时区,然后将此时间转换为我当地时间.

我该怎么做,请建议我!!

(仅供参考,目前,UTC时间是10:25 AM,印度当前时间是下午3:55)

Lie*_*yan 13

尝试使用TimeZone.getDefault()而不是TimeZone.getTimeZone("GMT")

来自文档:

...使用getDefault获取TimeZone,根据程序运行的时区创建TimeZone.

编辑:您可以使用SimpleDateFormat解析日期(还有格式字符串的文档).在您的情况下,您想要(未经测试):

 // 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);
Run Code Online (Sandbox Code Playgroud)

或者,使用两个单独的SimpleDateFormat实例,一个用于原始实例,另一个用于目标时间.