Android how to set time zone through application - android

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

Related

Android - Changing Phone's Timezone

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.

Android: getting the time

i am currently using the following line to achieve the time: System.currentTimeInMilis.
I have noticed it doesn't consider time zones,or does it not match the android phone it self by the time, while on the emulator it does match.s
so is there another type of way to get the android clock it self? so when the user adjusts he's phones built in clock, it affects it too?
float getTime()
{
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
cal.setTimeZone(TimeZone.getDefault());
return cal.getTimeInMillis();
}
Read the documentation of currentTimeMillis. It has a time zone, which happens to be UTC (which is the default for Unix time stamps).
If you want to convert it to a different time zone you can make use of the Java Calendar and TimeZone classes:
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
cal.setTimeZone(TimeZone.getDefault());
Alternatively you can just create a new GregorianCalendar instance. By default its TimeZone will match the local one (as set on the device) and the time will be set to "now".
There are also other ways for retrieving the current time according the current time zone and locale as string. Take a look at DateUtils.
EDIT Explaining the usage of Calendar
Read the documentation for Calendar.getTimeMillis(). That method returns the Unix time stamp again which happens to have the time zone UTC.
You have to use the Calendar.get() method instead for getting the correct values. See following example for getting the current hour in the correct time zone via your calendar object:
int hour = cal.get(Calendar.HOUR_OF_HAY);
Read the documentation of Calendar. There are plenty of fields like HOUR_OF_DAY which help you getting values like the year, month, minute, seconds etc.

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