How to get real time date from server in android? - android

I have one android application which will show random Quotes everyday from application's
database, but when i change date in my android phone, i will get the future quotes. To stop this i need current date and time from server side ?
Is there any URL or web service which gives real time data ?
Any help will be appreciated.

You can check this service : http://timezonedb.com/api. But its not recomended to use an external service for that.
You should probably use your own server.

Related

How to get the global time (not system specific time) in android

I would like to get the current global time in android, meaning, even if the user changed the system's time, I would like to get the correct time.
How can I do that?
To do that you must first have access to the location of the device.
You can connect through a web service such as:
https://timezonedb.com/
https://www.worldtimeserver.com/
Through its APIs you can know the date and time of the zone in which the device is located. If you need more information on how to read restful or xml file of the web service answer, tell me.
The best solution is:
If you have a server. Make your own web service that verifies that the date and time of the Android device on which your App is running matches that of your server.

Get datetime/timestamp from a website/url in android

here is what i'am trying to achieve, either one of the two would be ok
Get the date and time from a specific url/website like "www.mywebsite.com"
Get the date and time from my MSSQL Server
either one of the two would be ok, because all i need is to get the same time from the website/server's end, because i'am making an APP Version for the Mobile Website i'am making and i want to show a Clock or Date&Time Reminder in the App
i have no codes working as of now, i tried searching online in search engines and also in stackoverflow but i can't seem to find what i'am looking for, either i'am using the wrong words to search for it or there really isn't any way
the reason why i need the date is because i'am checking when the records inside the database was created and verifying how much time has passed since it was created, it like comparing (CreationDateTime & CurrentDateTime)
i can successfully Display how many hours have passed, i just need the CURRENT PROPER & ACCURATE DATE & TIME of the server :)
I can't help but think this is an XY problem, or at least close to it.
Your client device knows what time it is, because it stays in sync via NTP and whatever network it's attached to (and we already know you're attached to a network because otherwise, this whole thing falls apart). And it knows (or should know) what timezone it's in, so you can translate that to UTC or whatever alternative timezone you might need.
Your server(s) should also be keeping time synced via NTP, so drift between the client and server should be sub-second at worst.
IOW, the time (when corrected to UTC, which all date/time libraries I've used have a method built in to do) will almost always match between your client and server, so why bother with the complexity of "pushing" the current time from server to client?
I'm not seeing why you'd need to do this at all. Have the server tell the client when the reminder is set for, then let the client work out the rest. Anything else would be excessively chatty over the network.
To get the date in the format below, just convert todays date to a string
--07/06/2017
SELECT convert(varchar(10), getdate(), 103)
i have found an alternative answer that works 100% in getting date and time from SERVER instead of URL
SELECT GETDATE() AS CurrentDateAndTime
GETDATE()

How can I sync time from server in android application

Now I write android application that related with server and I don't know how to get time from server. How can I show server time in my application?
Thank you for your help.
get server time which return from a network call. then setTime in your calendar instance.

Get actual DateTime of a device in Offline Mode

I am developing an android application. I need to solve the below mentioned issue in my application:
1) The application can work in online as well as offline. The app has a feature to create and save the NOTES inside the application. Also, When the app gets internet connection, I need to send the NOTES to backend server. I have a field called 'DATECREATED' in each NOTE (the datetime where the actual NOTE was created)
ISSUE:
If the User has set the DateTime wrongly in the device, My application sends the incorrect DATETIME to server. I am wondering how could I solve the issue?
Any Ideas would be appreciable.
When you eventually post to your server, just add that specific time as well. new Date().getTime() will give you a value that you can use to compare with the datetime of your server. If it's for example 2 hours later, then just set the time of the post 2 hours back.
So send both the time when the post was submitted by the user and the time when it was actually sent.
You can get current date and time by using network time.
LocationManager locManager = (LocationManager) mContext
.getSystemService(mContext.LOCATION_SERVICE);
long lastSyncTime = locManager.getLastKnownLocation(
LocationManager.NETWORK_PROVIDER).getTime();
I have used it to get time, based on location in my application.
I think it will work in online and offline mode as well.

Retrieving the current date and time from a time server

In my Android app, I would like to retrieve the current date/time (UTC) from some server. Android does have the built-in ability to synchronize the device's date and time with some server but I have found this to be unreliable. Does Google or some other well known service provider provide a simple API that would allow me to retrieve the current date/time? I am not interested in trying to set the date/time on the device (that's not even possible). But I do need to know the correct date in order to perform scheduled tasks that depend on exact dates.
I have seen a device reset its date to something like 1980 when the battery has been removed for an extended period of time and therefore I cannot rely upon the date of the device.
EDIT:
I came across this post:
https://stackoverflow.com/a/13066268/753632
But it requires installing Apache Commons Net. Kind of overkill just to get the time. I don't need to bloat my code more than it already is.
you can refer this link How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?
You could use the calender class, like is explained here, although this uses the time of the device itself. You could also read this it explains how to get the time for the service provider.
I think using the calender and then calculating the UTC time with based on the timezone the phone is in (like this). Because the phone already gets it's time from a server when it is connected to the internet.
I hope this helps.
EDIT: I see you edited your post. So you don't want to use the device time... I think you could look into getting it from the service provider then.

Categories

Resources