简体   繁体   English

在Android中按位置更改“时间选择器”中的时间

[英]change time from Time picker by Location in android

I use this Code but not working properly in other time zone. 我使用此代码,但在其他时区无法正常工作。 How this time is changed by changing the time zone or current system time zone 通过更改时区或当前系统时区如何更改此时间

int hour; 整数小时 int minute; 分钟

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 16);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.AM_PM, Calendar.PM);


        TimePicker asarTime=(TimePicker)findViewById(R.id.asarTime);
        asarTime.setCurrentHour(16);
        asarTime.setCurrentMinute(30);

        Date currentLocalTime = cal.getTime();
        SimpleDateFormat date = new SimpleDateFormat("HH:mm:ss z");

        date.setTimeZone(TimeZone.getTimeZone("UTC"));
        String localTime = date.format(currentLocalTime);           
        TextView timeShow=(TextView)findViewById(R.id.textView1);

        timeShow.setText(currentLocalTime.toString());

First of all, set the timezone like this: 首先,按以下方式设置时区:

 TimeZone tz = cal.getTimeZone();
    date.setTimeZone(tz);

You could add a service that checks the timezone to update your application. 您可以添加检查时区的服务以更新您的应用程序。 Actually it doesn't happen too often, that an user has to change the timezone while using an application, so you can check this just everytime the application starts. 实际上,这种情况很少发生,用户在使用应用程序时必须更改时区,因此您可以在每次应用程序启动时进行检查。

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

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