Search code examples
kotlindatedatetimesharedpreferencesdate-difference

How can I check if 24 hours have passed between two dates in kotlin?


I need to check if 24 hours have passed between two dates. If 24 hours have passed, I will update the data in my room database, if not, I will not. How can I do that. I thought about keeping a date with shared preferences and comparing that date with the current date, but I don't know how to do that.


Solution

  • You need to check that the different between the date stored in shared pref as an Long and the current date are bigger than or equal the value of 1 day in millis

    var isDayPassed = (System.currentTimeMillis() - date) >= TimeUnit.DAYS.toMillis(1)
    
    

    Note: Make sure to import TimeUnit using import java.util.concurrent.TimeUnit