How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android(如何在 android 中将日期从 GMT 时区解析为 IST 时区和反之亦然)
问题描述
我正在开发一个从 IST(印度标准时间)的后端获取日期/时间的项目,如图所示 "2013-01-09T19:32:49.103+05:30"代码>.但是,当我使用以下 DateFormat 解析它时
I am working on a project that fetches Date/Time from backend in IST(Indian standard Time) as shown "2013-01-09T19:32:49.103+05:30". However when i parse it using following DateFormat
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
接着解析..
Date date = sdf.parse("2013-01-09T19:32:49.103+05:30");
System.out.println("XYZ ==============>"+date);
它以 GMT 格式显示日期作为输出,即
its Displaying date in GMT format as output i.e
Wed Jan 09 14:02:49 GMT+00:00 2013.
我已经尝试使用 TimeZone 类作为..
I have tried it using TimeZone class as..
TimeZone timeZone=TimeZone.getTimeZone("IST");
sdf.setTimeZone(timeZone);
但没有效果..
我怎样才能得到一个 Date 类对象,其日期为 IST 格式而不是 GMT...
How could i get a Date class Object having Date in IST format instead of GMT...
请提供适当的解决方案..
Please provide an appropriate solution..
这就是代码的样子:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
TimeZone timeZone=TimeZone.getTimeZone("IST");
sdf.setTimeZone(timeZone);
Date date = sdf.parse("2013-01-09T19:32:49.103+05:30");
String formattedDate=sdf.format(date);
System.out.println("XYZ ==============>"+formattedDate);
推荐答案
日期没有任何时区.它只是自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数的持有者.采用您用于解析的相同 DateFormat,设置 IST 时区并格式化您的日期,如下例所示
Date does not have any time zone. It is just a holder of the number of milliseconds since January 1, 1970, 00:00:00 GMT. Take the same DateFormat that you used for parsing, set IST timezone and format your date as in the following example
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = sdf.parse("2013-01-09T19:32:49.103+05:30");
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(sdf.format(date));
输出
2013-01-09T19:32:49.103+05:30
请注意,XXX 模式自 1.7 起用于 ISO 8601 时区 (-08:00).如果您使用的是 1.6,请尝试 Z.有关格式模式的详细信息,请参阅 SimpleDateFormat API
Note that XXX pattern is used for ISO 8601 time zone (-08:00) since 1.7. If you are in 1.6 try Z. See SimpleDateFormat API for details of format patterns
这篇关于如何在 android 中将日期从 GMT 时区解析为 IST 时区和反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 android 中将日期从 GMT 时区解析为 IST 时区和反之亦然
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
