2

Okay so this might be basic but unfortunately I haven't found anything yet to help with this.

I'd like certain functionality to only happen in Android during a time frame that is specific to UTC/GMT Time.

How do you do this? I tried using System.currentTimeMillis() but if you change the time on an Android Device in the settings, this will also change System.currentTimeMillis().

I'd like to grab a time that is equal to UTC/GMT AND is independent of the Android Device Settings clock, so if the clock is changed on the device it won't interfere.

Any thoughts?

Thanks! Christopher Steven

chocospaz
  • 739
  • 1
  • 8
  • 15
  • You could try this: http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java/6697884#6697884 – Chris Gomez Jul 21 '15 at 02:44
  • @cgomezmendez thanks for the suggestion but unfortunately it doesn't work; I just tested it in a sample app ... If you change the device time, this changes as well. – chocospaz Jul 21 '15 at 02:53
  • This : [Google-sntpClient-fetch utc from web](http://stackoverflow.com/questions/14771223/get-utc-date-from-web-in-android-application) – Samrat Dutta Jul 21 '15 at 02:55

3 Answers3

4

time.nist.gov is your friend if you want a truly accurate time.*

                String TIME_SERVER = "time.nist.gov";
                NTPUDPClient timeClient = new NTPUDPClient();
                InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
                TimeInfo timeInfo = timeClient.getTime(inetAddress);
                NtpV3Packet message = timeInfo.getMessage();

                //get the utc long from the server
                long serverTime = message.getTransmitTimeStamp().getTime();

(Be sure to bundle all this in a thread if you're running on Android!)

One thing that I like to do is to take the difference between the server time and system time and store that for future use. In short, as long as the system time isn't fiddled with, you don't have to do multiple time calls. You can just take system time and change it based on the difference.

*you will need apache commons for this to work.

Matter Cat
  • 1,538
  • 1
  • 14
  • 23
  • Thanks for an idea! At some point, I know I'll need to make a backend request, I can have the server pass me the server time in one of its headers, store that, and compare it with an elapsed time – chocospaz Jul 21 '15 at 02:58
0

You need a time service from Internet. you can get the real time from time server NIST

apache-commons has a TimeInfo class to get NTP date

Example: NTPClient

chengpohi
  • 14,064
  • 1
  • 24
  • 42
0

System.nanoTime() returns the number of nanoseconds since the start up time, and is therefore not be susceptible to system clock changes.

At the startup use:

long startTime = System.TimeInMillis();
long nanoToDeduct = System.nanoTime();

Then at any point in time you can get the current time of the program as the clock was set at the startup of the program with

long currentTime = startTime - timeToDeduct + System.nanoTime();
Genkus
  • 151
  • 6