Search code examples
androiddateandroid-date

Android get Date difference


I want to implement Question of the day feature in my application. If user use that feature once in a day he can not be able to access that till the day change. I am able to fetch when the feature access first but how can i check this date time for next time.

How can i get the date is changed or not.

Thanks in advance.


Solution

  • You can use a combination of timestamp, shared preferences and the use of method isToday(long when) located at android.text.format.DateUtils

    code example:

    SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);
    long sharedPreferenceTimestamp= userDetails.getLong("lastquestiontime", 0); // 0: 1/1/1970
    if(!DateUtils.isToday(sharedPreferenceTimestamp)) {
       // --> show the Question HERE! <--
    
       // update the value of last question show!
       SharedPreferences.Editor editor = userDetails.edit();
       editor.putLong("lastquestiontime", System.currentTimeMillis()); // value to store
       editor.commit();
    }