如何在android中设置TimeZone [英] How set TimeZone in android

查看:942
本文介绍了如何在android中设置TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处执行此操作,但仍未转换为所需内容:

Do so here, but still no translated into desired:

String dtc = "2014-04-02T07:59:02.111Z";
            SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date date = null;
        try {
            date = readDate.parse(dtc);
            Log.d("myLog", "date "+date);
        } catch (ParseException e) {
            Log.d("myLog", "dateExcep " + e);
        }

        SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm"); 
        writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
        String s = writeDate.format(date);

在变量s的输出仍然给出时间07:59:02,我想要提前+4小时,即11:59:02

At the output of the variable "s" still gives the time 07:59:02 , and I want to make it +4 hours in advance that is 11:59:02

推荐答案

你需要指示读取格式化程序将输入解释为UTC(GMT - 记住Z代表ISO-8601格式的UTC):

You need to instruct the read-formatter to interprete the input as UTC (GMT - remember that Z stands for UTC in ISO-8601-format):

String dtc = "2014-04-02T07:59:02.111Z";
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
readDate.setTimeZone(TimeZone.getTimeZone("GMT")); // missing line
Date date = readDate.parse(dtc);
SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm");
writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
String s = writeDate.format(date);

然后你会得到:

02.04 .2014,11.59

02.04.2014, 11.59

这篇关于如何在android中设置TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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