Skip to content Skip to sidebar Skip to footer

Comparing Current Time In Milliseconds To Time Saved In Shared Preferences

I've shared my current time in sharedPreferences using the following method: Date date = new Date(System.currentTimeMillis()); SharedPreferences pref = getApplicationContext().get

Solution 1:

1st of all, on this line you should change to:

editor.putLong("smstimestamp", System.currentTimeMillis());

there's no need to create a date and then get the millis again.

now to compare it:

//             milli min  hour  day 30day
long days_30 = 1000 * 60 * 60 * 24 * 30;
SharedPreferences pref = getApplicationContext().getSharedPreferences("DataCountService", 0);
long oldTime = pref.getLong("smstimestamp");
if(System.currentTimeMillis() - oldTime > days_30){
      // here, more than 30 days
}

Post a Comment for "Comparing Current Time In Milliseconds To Time Saved In Shared Preferences"