简体   繁体   中英

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.

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