I have some things in my Android application that need to update once per day.
It’s pretty simple I think, but I have no idea in what format I need to format the date and time (if time is needed) and how to check if an update has been done today (where today is between 00:01am and 23:59pm in the user’s local time). The update should not be done if it was already done for today.
Here’s what I DO know how to do:
- Save the last update date in SharedPreferences (but how do I get a
string of it, I do not know) - Get things from SharedPreferences (but I
don’t know how to compare dates in string format)
It is irrelevant what format you choose. It is just matter of recalculations.
I’d suggest using milliseconds since epoch, as all system calls use it, so it would be easier for you to use the same.
As 1000 millis is 1 second it’s easy to figure out that
1000*60*60*24equals to one day (24hrs). So, ifstoredMillisis bigger thanNOW - 1000*60*60*24, (andNOWis i.e.System.currentTimeMillis()), then it is too early to do the check. IfstoredMillisis smaller, then save yourNOWtimestamp and do the check:EDIT
It’s just the matter how to get the proper millis to compare against. Replace
long now = System.currentTimeMillis();from above code with following code block:which shall do the trick.