12

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.

Ashish Augustine
  • 1,736
  • 4
  • 19
  • 48

5 Answers5

19

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");
Synesso
  • 36,000
  • 33
  • 126
  • 200
5

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'"
Reck
  • 6,922
  • 2
  • 19
  • 24
4

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));
MarchingHome
  • 1,164
  • 8
  • 15
4
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

Community
  • 1
  • 1
Abhishek Chanda
  • 2,426
  • 6
  • 30
  • 59
  • 3
    That source is the answer to a different question and is not sufficient in this case. – MarchingHome Oct 17 '12 at 07:32
  • 1
    Thanks. This answer somehow saved my life, because I kept set time zone on calendar instance only but not SimpleDateFormat, thats why I cannot get my shifted time anyway. I am at zone +8 for example, but I want my app display time from zone -7, this is what I want. Change it "programmatically". Voted up. – elliotching May 26 '19 at 14:26
-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.

YusufY
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 12 '21 at 03:50
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30319186) – mochadwi Nov 13 '21 at 13:04