如何将YYYY-MM-DDTHH:mm:ss.SSSZ格式的时间转换为默认时区? [英] How to convert time in YYYY-MM-DDTHH:mm:ss.SSSZ format to default timezone?

查看:773
本文介绍了如何将YYYY-MM-DDTHH:mm:ss.SSSZ格式的时间转换为默认时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的时间格式为"2011-07-31T08:16:37.733Z".实际上,Z应该是时区,然后将该时间转换为本地时区.我现在该如何实际将该时间转换为默认时区.

The time that I am getting is in the format "2011-07-31T08:16:37.733Z". Actually the Z should be the timezone and then that time is converted to local timezone. How do I actually convert this time to default timezone now.

推荐答案

RFC 3339描述了特定的 ISO 8601 '配置文件'用于日期/时间表示.

RFC 3339 describes a specific ISO 8601 'profile' for date/time representation.

如果可以选择使用第三方开源库,请查看Joda-Time的

If using 3rd-party open-source libraries is an option, take a look at Joda-Time's ISODateTimeFormat utility class.

否则,由于ISO 8601/RFC 3339表示时区信息的方式,如果您需要推出自己的代码,仅使用 SimpleDateFormat 是不够的.

Otherwise, if you need to roll out your own code it's not enough to use a SimpleDateFormat because of the way ISO 8601/RFC 3339 represents timezone information.

如果您需要解析的所有内容都在"Z"(Zulu或UTC)中,则相对简单:

If all you need to parse is in 'Z' (Zulu or UTC), then it's relatively simple:

String input = "2011-08-11T01:23:45.678Z";
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
f.setTimeZone(utc);
GregorianCalendar cal = new GregorianCalendar(utc);
cal.setTime(f.parse(input));
System.out.println(cal.getTime());

应在您当地的时区打印出"2011-08-11T01:23:45.678Z".

Should print out "2011-08-11T01:23:45.678Z" in your local time zone.

如果您需要处理RFC 3339中的任意时区,则需要扩展上述内容以解析时区指示符并检索相应的Java TimeZone 实例.如果只是整小时+/- GMT,这应该不太困难,但是在非标准偏移量的情况下,您可能必须创建自己的 SimpleTimeZone 实例.

If you need to handle arbitrary timezones in RFC 3339 then you'll need to extend the above to parse out the timezone designator and retrieve the corresponding Java TimeZone instance. That shouldn't be too hard if it's just whole hours +/- GMT, but in case of non-standard offsets you may have to create your own SimpleTimeZone instance.

这篇关于如何将YYYY-MM-DDTHH:mm:ss.SSSZ格式的时间转换为默认时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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