Android - Changing Phone's Timezone - android

Is is possible to set the phone's timezone programmatically in android? I got this code
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()));
but it does not change the phone's timezone. It only displays the timezone of America/Los Angeles.

The code you are showing simply defines a Calendar instance with a specific format to be used in the app.
It is not possible to change the phone's timezone programmatically.
You could redirect the user to the appropriate settings:
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));

Well, this only changes formatting, so you cannot expect it to change phone's time zone. And no, there are no APIs for changing time or timezone, this is reserved for system apps.

Related

Is setTimeZone method is device independent?

My App is heavily dependent on time. If I set the time zone using below code and user device time/date is not set correctly then will it work correctly and return correct time?
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
This will not help if the device time is off. By setting the timezone, you are just telling the formatter to display (what the device thinks is) the current time with an offset of +7/+8 hours.

Android how to set time zone through application

Android provides a permission called "SET_TIME_ZONE" with OS permission level "dangerous". Does anyone know that given an application with this permission, how can the app set the time zone ?
Thanks.
If your objective is to change the system's default time zone, then use setTimeZone() of AlarmManager.
You can set the TimeZone in multiple ways:
You can use TimeZone.setDefault() which will change the TimeZone for the current process only. But as noted in the docs, this is not garanteed to last for the whole application lifecycle.
You can use setTimeZone() of AlarmManager to change the TimeZone of the whole device. But you need the "SET_TIME_ZONE"-permission for that.
If you think 1. is to dangerous and you don't have the permission for 2. your best approach is to get every Date from Calendar and set the TimeZone on your Calendar-instance via setTimeZone().
For Setting of the Time Zone Programetically you need to use the Date Class.
See its Reference Documents here.
You need to use the setTimeZone() method of SimpleDateFormat Class.
Following is sample code for settings Time Zone of 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's setTimeZone() Method
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

How to obtain user date time settings in android

I have a Calendar object, and i know how to format it using android.text.format()
How can i get the phone settings for date and time to format the object to format set by the user. Like 24 hour clock , mmm dd yyyy...
What I wanted was to provide the date and time formatted according to the user's preferences and locale, in separate TextViews.
I have done this using static methods in the DateFormat class:
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); // Gets system date format
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(getApplicationContext()); // Gets system time format
txt1.setText(dateFormat.format(CalendarInstance.getTime())); // format() Returns a string according to the user's preferences (12-hr vs 24-hr) and locale
txt2.setText(timeFormat.format(CalendarInstance.getTime()));
Well, not exactly all the settings (such as 24 vs 12 hour format) selected by the user, but to use the configured locale you can use the DateUtils class.

Setting TimeZone of Java Calendar to device timezone?

How can I construct a Calendar object using getInstance(TimeZone) to use the device's TimeZone?
According to this issue, just using:
Calendar calendar = Calendar.getInstance();
will give you an instance in the user's default time zone (as per their settings). You can determine the time zone from the calendar with:
TimeZone zone = calendar.getTimeZone();
Other posts have suggested that using TimeZone.getDefault() does not give this user-default time zone - I don't know about that personally, but it's another option to look into.

Formatting time on Android, while following preferences (24 hr clock vs. AM/PM, etc..)

I'm currently using the following code to format time on Google Android:
DateFormat.getDateTimeInstance().format(millis)
While this code honors my timezone and locale settings, it ignores the '24 hour mode' setting in system preferences, always returning time in AM/PM. Is there any way to get time in the same format as that on the status bar?
In theory (never tried this yet), you can use
http://developer.android.com/reference/android/text/format/DateFormat.html
To get formatters based on your settings. YMMV.
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
df.format(new Date(date));
"date" in millisec
Use the java.text.SimpleDateFormat class.

Categories

Resources