简体   繁体   English

如何在Android中处理时区和夏时制?

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

I'm writing this application which needs to handle DST in android. 我正在编写此需要在android中处理DST的应用程序。 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. 如果我运行该应用程序并显示当前时间,然后转到android设置并在不退出该应用程序的情况下更改时区,然后从该应用程序获取当前时间,则不会给我新的时间更改。

UPDATE Following is the code UPDATE以下是代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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