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

时间:2012-11-14 08:48:42

标签: android

我使用此代码 但在其他时区无法正常工作。如何通过更改时区或当前系统时区来更改此时间

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

1 个答案:

答案 0 :(得分:0)

首先,像这样设置时区:

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

您可以添加检查时区的服务以更新您的应用程序。 实际上,用户不得不在使用应用程序时更改时区,因此您可以在每次应用程序启动时进行检查。

相关问题