Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change timezone in android programmatically

Tags:

android

I wanted to change timezone in android programmatically like to set timezone as "America/Los_Angeles". How can I do this. How can this be possible by using id's.

like image 958
Ashish Augustine Avatar asked Mar 06 '12 02:03

Ashish Augustine


People also ask

How do I change the TimeZone in android programmatically?

Call TimeZone. getAvailableIDs() to get the list of known timezones for your system. This permission is only allowed for system apps. on android api 26 and 27 receive this error: setTimeZone: Neither user 10087 nor current process has android.

How do I set UTC time zone on Android?

On a current Android device, tap the Clock app, tap the Globe icon (bottom of the screen), then search for UTC and tap the UTC result. On a current iOS device, tap the Clock app, tap World Clock, then + (in the upper-right corner), search for UTC, then tap the UTC result.

How does Android determine TimeZone?

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time. Hours must be between 0 to 23 and Minutes must be between 00 to 59.

How do I set the default TimeZone in Intellij?

#Step 2: Set timezone in Intellij Under VM options, type the timezone you want. Example: -Duser. timezone="UTC" if you want UTC.


4 Answers

In your AndroidManifest.xml

<uses-permission android:name="android.permission.SET_TIME_ZONE"/>

In your source:

AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
am.setTimeZone("America/Los_Angeles");
like image 152
Synesso Avatar answered Oct 20 '22 21:10

Synesso


If this is on a simulator or a device that you have root on, you could run

adb shell "su -c 'setprop persist.sys.timezone America/Los_Angeles; stop; sleep 5; start'"
like image 26
Reck Avatar answered Oct 20 '22 19:10

Reck


Abhishek's code simply defines a Calendar instance with a specific format to be used in the app, so that will not work.

It is not possible to change the phone's timezone programmatically. You could redirect the user to the appropriate settings, however:

startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
like image 25
MarchingHome Avatar answered Oct 20 '22 21:10

MarchingHome


Calendar calendar = Calendar.getInstance();       
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println(sdf.format(calendar.getTime()));

Source

like image 29
Abhishek Chanda Avatar answered Oct 20 '22 20:10

Abhishek Chanda