Search code examples
androidpreferences

Compare long date format from preference


I can get file's Last Modified date and time, I would like to compare it with my last modified date and time from preference, but unfortunately it's always true.. Did I made a mistake?

 /* Get Last Update Time from Preferences */
    SharedPreferences prefs1 = getPreferences(0);
    long lastUpdateTime = prefs1.getLong("lastUpdateTime", 0);

    File file = new File(filePath);
    Date lastModDate = new Date(file.lastModified());
    long curMillis = lastModDate.getTime();

    /* Should Activity Check for Updates Now? */
    if ((lastUpdateTime) <= curMillis) {

        /* Save current timestamp for next Check */
        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putLong("lastUpdateTime", curMillis);
        editor.commit();

        do somthing....

    } 

Solution

  • Your lastUpdateTime would be always equal to currMillis because after your update you always set it equal to currMillis.

    Your check should be -

    if (lastUpdateTime < currMillis)
    

    This will execute only if the file has been updated after the last check was performed because otherwise even if the file was not updated then also currMillis would be equal to lastUpdateTime so (lastUpdateTime <= currMillis)would evaluate to true but it should be false.