根据当前时区显示UTC日期/时间到日期/时间 [英] Display UTC date/time into date/time according to the current time-zone

查看:127
本文介绍了根据当前时区显示UTC日期/时间到日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网上收到日期/时间字符串,格式为yyyy / mm / dd'T'HH:MM:SS'Z,而且是UTC。



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



我该怎么做建议我!!



(FYI,目前UTC时间是10:25 AM,印度当前时间是3:55 PM)

解决方案

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



文档


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


编辑:您可以解析日期使用 Simp leDateFormat (还有格式字符串中的文档)。在你的情况下,你想做(未经测试):

  //请注意,我稍微修改了格式字符串
SimpleDateFormat fmt = new SimpleDateFormat(yyyy / MM / dd'T'HH:mm:ss'Z);
//将时区设置为原始日期字符串的时区
fmt.setTimeZone(TimeZone.getTimeZone(GMT));
日期date = fmt.parse(1998/12 / 21T13:29:31Z,新的ParsePosition(0));

//然后重置为目标日期字符串的(本地)时区
fmt.setTimeZone(TimeZone.getDefault());
String localTime = fmt.format(date);

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

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 !!

(FYI, Currently, UTC time is 10:25 AM, in india current time is 3:55 PM)

解决方案

Try using TimeZone.getDefault() instead of TimeZone.getTimeZone("GMT")

From the docs:

... you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running.

EDIT: 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);

alternatively, use two separate instances of SimpleDateFormat, one for original and one for target time.

这篇关于根据当前时区显示UTC日期/时间到日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆