14

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,784
  • 4
  • 20
  • 50

5 Answers5

21

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
  • 37,610
  • 35
  • 136
  • 207
7

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,184
  • 9
  • 15
  • According to info from Synesso and HHLV, it is possible to set the time zone. Need to setup a permission and must be a system app. IDK how to setup as system app. Might not be possible for a regular (play store) app. But, I assume possible in some situations (i.e. embedded). – steve Aug 01 '23 at 15:27
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
  • 7,966
  • 2
  • 20
  • 24
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
ACC
  • 2,488
  • 6
  • 35
  • 61
  • 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