android - 我如何将 ISO 时间转换为 android 中的 IST 和 GMT?

标签 android

这是我将 ISO 转换为 IST 以及将 ISO 转换为 GMT 的代码。

Log.e("ISO TIME",""+time);//The time which i got from JSON file.
            //IST TIME ZONE
            Date date=new Date();
            DateFormat format=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat();
            simpleDateFormat.applyPattern("yyyy-MM-dd'T'HH:mm'+00':ss");
            date = simpleDateFormat.parse(time.toString());
            format.setTimeZone(simpleDateFormat.getTimeZone());
            Log.e("IST TIME", "" + format.format(date));

            //GMT TIME ZONE
            Date date1=new Date();
            SimpleDateFormat sdf=(SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
            sdf.applyPattern("yyyy-MM-dd'T'HH:mm'+00':ss");
            date1=sdf.parse(time);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            Log.e("GMT TIME", "" + sdf.format(date1));

这是我的输出

E/ISO TIME: 2016-01-18T08:40+00:00
E/GMT TIME: 2016-01-18T03:10+00:00
E/IST TIME: Jan 18, 2016 8:40:00 AM India Standard Time

问题是 IST 和 GMT 之间的实际差异是 5:30 但在我的输出中,我得到了恰好 5:30 的差异

请帮我解决这个问题。

最佳答案

@Logic 绝对正确。 但我有一些建议要给你。

您需要在 GMT 时间上加上 5:30,然后您将获得 IST。 永远不要在您的 IST 时间中增加 5:30 小时。

看这个例子 你的 IST 时间是 8:40,GMT 时间是 3:10

每次操作增加 1 小时

在圆括号中,每次迭代增加一小时:

3:10->4:10(1)->5:10(2)->6:10(3)->7:10(4)->8:10(5)

完成一小时的剩余 IST 时间分钟数是 00:20

将 00:20 添加到 8:10 将成为 8:30。

添加需要GMT时间 3:20

你的代码绝对正确

关于android - 我如何将 ISO 时间转换为 android 中的 IST 和 GMT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34899492/

相关文章:

Android 应用单实例

android - 如何嵌套字符串资源?

android - 方向改变时应用程序崩溃

android - 如何接收蓝牙设备工作的 Intent ?

android - 如何在TypeConverter中使用Gson将Kotlin枚举与Room存储在一起?

Android Appcompat v21 - 使用旧的微调器样式

android - FragmentPagerAdapter notifyDataSetChanged 不起作用

android - fragment 中的错误非默认构造函数

android - 如何在没有 DDMS 的情况下打开 .trace 文件格式(traceview)?

java - 即使设备所有权设置为此应用程序,也允许使用 pin 取消固定应用程序