Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get difference between old and new time

Tags:

android

I need to know the difference value between old and new system time (after user changed it)

With following code I catch moment when user changed time:

<receiver android:name=".TimeChangeReceiver">
    <intent-filter>
        <action android:name="android.intent.action.TIME_SET"></action>
    </intent-filter>
</receiver> 

public class TimeChangeReceiver extends BroadcastReceiver {
    private static final String TAG = TimeChangeReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "changed time: " + new Date(System.currentTimeMillis()));
    }
}

For example current time 10:00, user changed it to 11:00 - onReceive method called, but time already changed, how to get previous value - 10:00?

like image 293
Sinigami Avatar asked Dec 11 '22 05:12

Sinigami


1 Answers

Below you find a solution to calculate the previous value. I tried it out and you can find my implementation here https://github.com/chrisport/setTimeIntent

The main idea is this: You need a independent time to compare to, we can take SystemClock.elapsedRealtime() which is the time since the device booted. Now we can calculate the difference between this elapsedTime and System.currentTimeMillis(). This difference will always be constant, unless the SystemTime has changed (e.g. by the user). In this case, we can calculate the new difference and compare them to determine how much the it has changed.

Let's do it:

  1. Calculate the current difference:

    public static long getCurrentDifference() {
      long elapsedTime = SystemClock.elapsedRealtime();
      long currentTime = System.currentTimeMillis();
      return currentTime - elapsedTime;
    }
    
  2. Store this difference at begining* to SharedPreferences

  3. When SET_TIME is received, calculate the difference between the current difference and the stored difference

    long lastDifference = mySharedPrefs.getLastTimeDifference();
    
    //Calculate the new difference
    long currentDifference = getCurrentDifference();
    
    //Calculate the difference of the differences
    //this is the change the user made
    long userChangeInMillis = lastDifference - currentDifference;
    
    //and here we get the old time:
    long previousTime = System.currentTimeMillis() + userChangeInMillis;
    

You can also make a Date out of it (new Date(previousTime)).

*To always have an initial time difference, you can store it to SharedPreferences when the app is opened, when the device is booted (needs another BroadCastReceiver), as well as save the newly calculated difference after SET_TIME has been received.

Edit:

The output of my example looks like this:

Before it was Wed May 21 17:43:28 GMT+00:00 2014
Now it is:    Wed May 21 18:43:00 GMT+00:00 2014

Somehow SET_TIME is received twice. Therefore there are two Log-messages and the second one shows twice the current time, because I overwrite the lastDifference with the newly calculated one.

like image 117
Chrisport Avatar answered Jan 16 '23 06:01

Chrisport