Alex Rosa
Alex Rosa

Reputation: 49

In Android system, for every change in date and time, is there a way to know if the user has advanced or delayed the device time or date?

The application already receive the system intent ACTION_TIME_CHANGED. but I need a way to identify if the user advance or delayed date or time.

the scenario is the following:

... and so on...

Id like to identify the type of date change (backward or forward) based on the last change, and not from the present date from a web provider.

Upvotes: 0

Views: 61

Answers (1)

khoshrang
khoshrang

Reputation: 156

I guess you want to know if the date and time in user's device is true and present in that case you can use a time API to get time from a web provider and compare it to the devices time.

Instant onThisMachine = Instant.now() ;
Instant remote = Instant.parse( stringFromRemoteService ) ;
Duration delta = Duration.between ( remote , onThisMachine ) ;
boolean thisMachineIsBehind = delta.isNegative() ;

These classes are built into Android 26+. For earlier Android, use the latest tooling to access most of the java.time functionality via “API desugaring”.

Upvotes: 1

Related Questions