Prashant
Prashant

Reputation: 1469

How can we get current date and time (IST) without system and server in android?

We are developing an android application in which we need current date and time. We are using APIs to get date and time from server. But we don't want to communicate all the time. Actually, we have 3 tabs. On every tab we are asking server to give date and time.and we have time drop down on every tab. We are populating drop down in such manner if current time is 4:50PM then we are populating drop down with half hour interval (i.e. 5:30PM,6:00PM,6:30Pm,7:00PM so on). Due to this reason tab swipe is became crappy. When I am swiping for next tab, then call goes to server for date and time in background. Till it doesn't get current date and time. my application gets hanged.

And We cannot use system's date and time for it. Because user can change the date and time for device.

I even checked for network provider for same. It is providing in case of SIM available. Its not working in case of Wi-Fi device only.

Is there any way to get current date and time ?

Thanks in advance!

Prashant.

Upvotes: 0

Views: 764

Answers (4)

JohannisK
JohannisK

Reputation: 537

Just a sugestion, and I'm not an Android Developer, so please correct me if i'm wrong!

http://developer.android.com/reference/android/os/SystemClock.html

You could use uptimeMillis from the system.

When the app starts, call a server to get the actual time, after that, use the difference in uptimeMillis to calculate the exact date and time without the need of an extra servercall.

Out of curiosity (because I'm not an android developer) What would happen if a user opens the app, timediff is calculated, moves app te background, changes device time and then reopens the same "instance" from background?

Upvotes: 1

sam
sam

Reputation: 4397

Get the time from server only once at startup, save locally the difference with device time and from here you can deduce other needed times based on the device time:

Tab time = device time - server time diff + tab diff time

Upvotes: 0

hkN
hkN

Reputation: 153

You could verify time and date when you connect to the server and register the difference with the system date.

Then you will be able to populate the drop down with that and correct the drop down when receiving an update from the server.

Upvotes: 3

Neal Ahluvalia
Neal Ahluvalia

Reputation: 1548

May be this helps

Time today = new Time(Time.getCurrentTimezone());
today.setToNow();

Not so sure but worth a try as my device is on wifi and this works in a tablet without a sim

Upvotes: 0

Related Questions