0

I want an app to do particular thing once in a day, when user opens the app first time in day. To do so I want to store a date in SharedPreference. But it only stores int, boolean, string etc.

/*

I'm showing my logic here. If there is better way plz tell me!!!
I will give some by default value like 00/00/0000 for initialization.
The first time user starts the app then I will do that particular work and initialize sharedPreference by current date 16/03/2020. 
If user again opens app by same date I don't do that work(By checking curdate and sharedPreference date).
If user opens app on another date I check currentDate and sharedPreference date, if they are different I will do that work and edit the sharedPreference by that day.
*/

So tell me how can store date in sharedPreference?

1 Answers1

0

You can store date as timestamp:

long timest = (new Date()).getTime();
// save it to Preferences as Long

and then read it and convert to Date again:

long timest = // read from Preferences as Long
Date date = new Date(timest);
S-Sh
  • 3,564
  • 3
  • 15
  • 19