TextClock setTimeZone() not changing TimeZone - android

I have a TextClock in my app. I want to give the user the option of the TimeZone it displays (rather than just using the device default).
I'm using the code
textClock.setTimeZone("PST");
Which according to the API documentation should change the TimeZone of the clock to whatever the string timezone identifier is (in this case Pacific Daylight Time) but the clock isn't changing. Interestingly though it isn't showing the current time either. I'm in the UK so we are using GMT+1 and if I don't .setTimeZone() it shows that but if I do it shows GMT+0 regardless of the timeZone I enter.

Turns out that despite the documentation saying you can use three letter identifiers (or maybe I picked that up wrong), you can't.
textClock.setTimeZone("America/Los_Angeles");
That works for setting to PDT for example.

Related

Detect current Android device timezone (not the user's preferred) as it travels

Many questions about timezones in Android were already asked here on SO,
however after going through all of them I still don't have the definitive answer why the following is happening:
Let's say I live in Germany, therefore have the preferred timezone offset +1. Then I decided to go to the US. While in the US, my device sends a message to the server with the timestamp local to where I am now in the US.
I use the following code to get the timestamp:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSZ");
fmt.setTimeZone(TimeZone.getDefault());
String timeStamp = fmt.format(new Date());
the timestamp I get is for example: 2016-02-20 08:32:01.000769+01
while 08:32:01 is the right time at the place where I am at that moment, the offset is still +1, whereas it should be rather something like -6.
From the answers here on SO, I got the impression that the Android devices don't care about the current timezone (given by my current location) and still insist on the original preferred timezone.
So am I right the only way to get this correct is to query some server with my coordinates to get my current timezone?
How come the device automatically syncs with the current network time as I travel, but does not sync the timezone offset? The cell towers should know in which timezone offset they are operating.
How come the device automatically syncs with the current network time
as I travel, but does not sync the timezone offset? The cell towers
should know in which timezone offset they are operating.
Yes they do and they usually provide this too. That's why you got related settings in your phone's "Date and Time" section. And this includes time zone as well. Yet, user must have this enabled and this value must be provided by towers which, again, usually works.

How does Automatic date and time work in Android?

For some date-based calculation I needed today's correct date, for which I enabled Automatic date and time option in Date and Time Settings and this gives me the correct time and date.
I want to know how Android gets the correct date and time, even though I have not enabled any internet accessibility (WiFi, SIM Data).
Also look at this: There is no response from requestLocationUpdates
Please give me some reference on this.
It capture date and time from the network provider. not only android lots of other phone can do the same.
Autometic date time is provided be the network provide (exmp: vodafone,airtel etc etc ).

One month restiction for application free use

Requirement:
In my application I want to allow user to free use all features for one month from the day of application's first run. For this I have store first run date in application database and compare first run date with current date every time when application launch.
Implementation:
For getting current date we have several function like Calendar.getInstance(); Date date=new Date(); these function will return the date\datetime of the standard "wall" clock (time and date). According to document Which can be set by the user or the phone network.
The Problem:
Every thing seems to be work fine but what if user change date from Setting and set it to past date. For example user have first run application on 7 June 2013 so after 6 July 2013 application must show that he have to purchase subscription, but if user change date of device back to 30 June etc. then the restriction will not work.
So is there any why to implement it correctly?
Is there any why to get actual time of device that is not be editable by user?
Edit:
Application can work offline, so user can turn off internet connectivity.
How about storing two dates - the date the app was first used and the date it was last used. 30 minus the difference between the two dates will give you the number of days remaining and if the current date is earlier than the last used date you know that they have changed the date back.
Just an idea - not tried it but in theory it should work.
Store the first day it was used and the last day it was used, and listen to the ACTION_TIME_CHANGED intent. Also, try using an AlarmManager.
Or if you have network, store it in a database off the phone.
But you probably need to ask yourself if someone really would change the date of their phone just to use your app. It would seriously mess up a lot of things for them: alarms, calenders, syncing to other services and so on. I don't think it is a problem in reality.
If it's a possibility, you could use a one-shot GPS request to get a time value. The user can't change the GPS time that it receives. Adding the permission is a pain if you don't need it, but this would prevent bad behavior caused by the user mucking with the date/time.
Event(APP_INSTALL) -> storeOnServer(INSTALL_TIME_KEY,getTimeFromServer());
Event(APP_LAUNCH) -> IF(getTimeFromServer -getStoredTime(INSTALL_TIME_KEY) >30)
-> showTrialPeriodEndedAlert()
-> quitApp();

How to set custom TimeZone in Android

In my Application I am displaying some info to the users according to the user's TimeZone.
I am allowing the users to select different cities in the world and to get the required info. By default I will get the TimeZone of the user from their Device using
`TimeZone myTZ = TimeZone.getDefault();`
And once the user changes the TimeZone , I am saving it in preferences. No problem in storing and retrieving the values from the preferences.
When another TimeZone is selected I set that TimeZone as default TimeZone using
`TimeZone.setDefault(TimeZone.getTimeZone("my_timezone"));`
when I print the TimeZone in the Log I am getting the TimeZone value as I set it. But when I use that TimeZone in my calculations I am getting previous TimeZone Value.
So I think the problem is the TimeZone is not changing , I want the user to set the selected TimeZone as their default TimeZone in their device. How shall I do this. What is the mistake I am doing?
Getting stuck in this for three days. Any suggestions to get out from this? Quick suggestion will helps me a lot. Thanks in Advance!!
As per documentation for TimeZone.setDefault(...) -
Overrides the default time zone for the current process only.
Warning: avoid using this method to use a custom time zone in your
process. This value may be cleared or overwritten at any time, which
can cause unexpected behavior. Instead, manually supply a custom time
zone as needed.
Apparently to change timezone through code you have to use AlarmManager. See the discussion here.
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setTimeZone(timezone);
It requires this permission to be set in the manifest
<uses-permission android:name="android.permission.SET_TIME_ZONE"></uses-permission>

Set default time zone to use for an android application

I'm developing an application for android that has to list activities with its date and time. I would like to ignore the time zone of the device and show always the time in timezone +02:00.
The application is for Spain, but If someone from UK that is in another time zone use the application I want that the time for activities appear in local spanish time.
The date is in UTC format, using Calendar.getTime().toLocaleString(), the time appers in local time zone of device.
I would like to know if the is a way to set a default time zone that has to use my application, and when I invoke Calendar.getTime().toLocaleString() method get the time always in local time zone +2 despite of the device timezone
TimeZone.setDefault(TimeZone.getTimeZone("GMT+3"));
This sets the default timezone. Subsequent calls to Calendar.getInstance() always return as if the device was in that timezone.
You need to set the time zone of your calendar.
This can be done like this :
Calendar.setTimeZone(TimeZone timezone);
And then your calendar will work with a different time zone.
If you want to change the system time zone this can be done by using setTimeZone() of AlarmManager.
When doing that the manifest has to contain the right permission, which is
android.permission.SET_TIME
Link to Documentation
And there is some other solution i found Here, check it out.

Categories

Resources