Ashish Augustine
Ashish Augustine

Reputation: 2040

Change timezone in android programmatically

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.

Upvotes: 15

Views: 31768

Answers (5)

YusufY
YusufY

Reputation: 1

You should have system signatures for setting timezones. if you are not manifacturer for your device may be you can contact them and ask for keystore files. after that you can sign your app as system with keystore file.

Upvotes: -1

Synesso
Synesso

Reputation: 39018

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

Upvotes: 23

Reck
Reck

Reputation: 8792

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'"

Upvotes: 6

MarchingHome
MarchingHome

Reputation: 1214

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

Upvotes: 8

ACC
ACC

Reputation: 2570

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

Upvotes: 4

Related Questions