Ashish Dwivedi
Ashish Dwivedi

Reputation: 8186

Get Real Time - Not Device Set Time in android

How to find out the Real Geographical Time, as in my application I want to attach the real time with my application.

So that if the user changes the time in his device then I should get only the real prevailing time - NOT THE DEVICE TIME

My question is much confusing but try to understand in a simple language -

I dont want DEVICE TIME, I want REAL PREVAILING TIME at any instant

Upvotes: 32

Views: 14921

Answers (3)

Mahmoud Mabrok
Mahmoud Mabrok

Reputation: 1482

Next code worked with me to get real date even if device has wrong date. Thanks to @Stefano.

import org.apache.commons.net.ntp.NTPUDPClient
import java.net.InetAddress
import java.text.SimpleDateFormat
import java.util.*


object DateOperation {

    private const val TIME_SERVER = "time-a.nist.gov"
    const val DATE_PATTERN = "yyyy-MM-dd"

    suspend fun getRealDay(): String {
        val timeClient = NTPUDPClient()
        val inetAddress: InetAddress = InetAddress.getByName(TIME_SERVER)
        val timeInfo = timeClient.getTime(inetAddress)
        val returnTime = timeInfo.message.receiveTimeStamp.date
        val t = SimpleDateFormat(DATE_PATTERN, Locale.ENGLISH)
        return t.format(returnTime)
    }
}


after adding dependency:

implementation 'commons-net:commons-net:3.6'

Then use it like

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch(Dispatchers.IO) {
            val date = getRealDay()
            Log.d("TestTest", "onCreate: $date")

        }

    }

`

Upvotes: 0

Gaurav Arora
Gaurav Arora

Reputation: 8362

You need to use the NTP (Network Time Protocol) protocol:

Here is some code I found somewhere else... and I am using it. This uses the Apache Commons library, which can be installed using Gradle (adding a dependency on commons-net:commons-net:3.6)

If you need a list of time servers, check: http://tf.nist.gov/service/time-servers.html

Here is some Java Code for you to use:

public class TimeLookup {
    public static final String TIME_SERVER = "time-a.nist.gov";

    public static void main(String[] args) throws Exception {
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        long returnTime = timeInfo.getReturnTime();
        Date time = new Date(returnTime);
        System.out.println("Time from " + TIME_SERVER + ": " + time);
    }

}

Note the Android code must run on the background thread and include gradle dependency:

implementation 'commons-net:commons-net:3.6'

Upvotes: 14

Raghav Sood
Raghav Sood

Reputation: 82553

There is no Android API to get you such a time. The user can always change the device time, nothing you can do about that.

Your best bet would be to run a server with a webservice, from which you can get the time. Since you control the server, you can try to ensure it returns the correct time.

Upvotes: 5

Related Questions