简体   繁体   中英

How to handle Time Zone and Day Light Saving in android?

I'm writing this application which needs to handle DST in android. I've written a small test code that would do the following. In addition I'm not using network provided date time in this case.

If i run the application and display the current time and go to android settings and change the time zone without exiting the application and then get the current Time from the application it wouldn't give me the new time change.

UPDATE Following is the code

btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Date dt = new Date(System.currentTimeMillis());
                txtViewStart.setText(sdf.format(dt));
            }
        });

        btnEnd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Date dt = new Date(System.currentTimeMillis());
                txtViewEnd.setText(sdf.format(dt));

            }
        });

How can I handle this issue as well as in case of Day Light Saving as well

        TimeZone timezone = TimeZone.getTimeZone(selectManulTimeZoneString);
    OR
    TimeZone timezone = TimeZone.getDefault();
String TimeZoneName = timezone.getDisplayName();
int TimeZoneOffset = timezone.getRawOffset() / (60 * 60 * 1000);
double hoursDiff = timezone.getDSTSavings() / (60 * 60 * 1000);
Log.i("AM","DayLightSaving:" + hoursDiff);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM