code为GMT转换为设备的时区不工作 [英] code for converting GMT to device time zone not working

查看:199
本文介绍了code为GMT转换为设备的时区不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从RSS源在以下格式输入时间:

I have a time input in the following format from a RSS feed:

周三6月13日17时05分44秒+0000 2012

和我需要为输出的周三2012年6月13日22时35分44秒

源时间将始终处于GMT,以及所需的输出时间将在设备时区(也可能是GMT + 5:30 GMT-2:00或任何)。

The source time will be always in GMT, and the required output time will be in the device time zone(it may be GMT+5:30 or GMT-2:00 or any).

所以,首先我有一个GMT日历实例,如下所示。

So firstly I have an calendar instance with GMT, as follows.

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

然后修改日历像输入时使用的StringTokenizer以下。

Then modified the calendar like following using StringTokenizer on input time.

calendar.set(Calendar.DAY_OF_MONTH, date);
calendar.set(Calendar.MONTH, month);
.
.
etc.

接下来,我有以下的code:

Next I have the following code:

calendar.setTimeZone(TimeZone.getDefault());

基本上上述code改变日历到设备的时区。
现在,不管是高于code为的在正常环境下工作正常,但在Android的的不工作。
任何解决方案?请帮助。

Basically the above code changes a calendar into device time zone. Now the matter is the above code is working fine in normal environment, but not working in Android. Any solution? Please help.

推荐答案

首先,你需要的的DateFormat 日期对象解析字符串值,然后您可以在设定的日期时区,以及你可以日历使用对象日期的帮助,日历对象将是您的设备时区实例。

First you required DateFormat to parse string value in Date object and then you can set Timezone in Date as well as you can make Calendar object with help of Date that calendar object will be your device timezone instance.

下面code是工作在我身边。

Below code is working at my side

    String input_format = "EEE MMMMM dd HH:mm:ss Z yyyy";
    String input_value="Wed Jun 13 17:05:44 +0000 2012";
    Date date=null;

    SimpleDateFormat sdf = new SimpleDateFormat(input_format);
    try {
        date = sdf.parse(input_value);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar calendar = sdf.getCalendar();
    calendar.setTime(date);

这篇关于code为GMT转换为设备的时区不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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