显示UTC日期/时间到日期/时间根据当前时区 [英] Display UTC date/time into date/time according to the current time-zone
问题描述
我得到一个日期/时间字符串从网页中的格式。YYYY / MM / dd'T'HH:MM:SS'Z',它是在UTC
I am getting a date/time string from web in the format of "yyyy/mm/dd'T'HH:MM:SS'Z'" and it is in UTC.
现在我要确定设备的当前时区,然后这个时间转换为我本地时间。
Now i have to identify the current time zone of device and then convert this time to my local time..
我如何做到这一点,请给我建议!
How do i do it, pls suggest me !!
(仅供参考,目前,UTC时间是10:25 AM,印度当前时间是3:55 PM)
(FYI, Currently, UTC time is 10:25 AM, in india current time is 3:55 PM)
推荐答案
尝试使用 TimeZone.getDefault()
而不是 TimeZone.getTimeZone(格林尼治标准时间)
从的文档:
...你用一个TimeZone getDefault它创建一个TimeZone 根据该时间区,在那里 程序正在运行。
... you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running.
编辑:您可以使用的SimpleDateFormat 解析日期(也有对格式的文档字符串有)。在你的情况,你想要做(未经测试):
You can parse date using SimpleDateFormat (there is also the documentation on the format string there). In your case, you want to do (untested):
// 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的实例,一个是原来的,一个是目标的时间。
alternatively, use two separate instances of SimpleDateFormat, one for original and one for target time.
这篇关于显示UTC日期/时间到日期/时间根据当前时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!