Android how to set the time zone through the application

Android grants the permission "SET_TIME_ZONE" with the OS permission level "dangerous". Does anyone know that given an application with such a resolution, how can the application set the time zone?

Thanks.

+6
source share
3 answers

To set the time zone programmatically, you need to use the date class. See His reference documents here .

You need to use the setTimeZone() method of the SimpleDateFormat class.

The following is an example code for setting the time zone according to America

 // First Create Object of Calendar Class Calendar calendar = Calendar.getInstance(); // Now Set the Date using DateFormat Class SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z"); // Finally Set the time zone using SimpleDateFormat Class setTimeZone() Method sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
+1
source

If your goal is to change the default time zone of the system, use setTimeZone () AlarmManager.

+14
source

You can install TimeZone in several ways:

  • You can use TimeZone.setDefault () , which will change TimeZone only for the current process. But, as noted in the docs, this is not guaranteed for the entire application life cycle.

  • You can use setTimeZone () AlarmManager to change the TimeZone of the entire device. But for this you need the parameter "SET_TIME_ZONE".

If you think that 1. is dangerous and you do not have permission to 2. your best approach is to get each Date from Calendar and set TimeZone in the calendar instance via setTimeZone () .

+3
source

Source: https://habr.com/ru/post/911017/


All Articles