code for converting GMT to device time zone not working

馋奶兔 提交于 2019-12-24 16:31:25

问题


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

Wed Jun 13 17:05:44 +0000 2012

and I need output as Wed Jun 13, 2012 22:35:44

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).

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

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

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

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

Next I have the following code:

calendar.setTimeZone(TimeZone.getDefault());

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.


回答1:


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.

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);



回答2:


if i understand it right, you just want to set the timezome

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setTimeZone("Europe/Paris");

this for example set the timezone to Paris



来源:https://stackoverflow.com/questions/11201793/code-for-converting-gmt-to-device-time-zone-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!