androidtimepickerdialog

Change TimePickerDialog to show time of specific country


I have a "TimePickerDialog", i want open that time picker with the current time of a specific country. Please share your suggestions.


Solution

  • hope it solve your issues. I added a list of GMT list. hope it will help:) its pretty straight forward solution but feel free to ask anything.

        //---------- Print time zone of Canada - Toronto -------------
        // for more GMT offset list :
        // https://greenwichmeantime.com
    
        java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT-5");
        Calendar c = Calendar.getInstance(tz);
    
        System.out.println(c.get(java.util.Calendar.HOUR_OF_DAY)+":"+c.get(java.util.Calendar.MINUTE)+":"+c.get(java.util.Calendar.SECOND));
        SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
    
    
        TimePickerDialog picker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // do what ever you would like to do :)
            }
        },c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),true);
    
        picker.show();