【发布时间】:2020-03-16 09:07:44
【问题描述】:
I asked same question before but it got closed and associated with another question which not solving my problem.
当用户在一天中第一次打开应用程序时,我希望应用程序每天执行一次特定的操作。为此,我想在 SharedPreference 中存储一个日期。但它只存储 int、boolean、string 等。
/*
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.
*/
那么告诉我如何在 sharedPreference 中存储日期?
【问题讨论】:
-
你要存储什么样的数据
-
您需要将
Date转换为String并存储到SharedPreference。如果您想获取该日期,请从SharedPreference获取日期并将String转换为Date -
您可以将对象转换为json字符串然后存储
-
只需将数据转换为字符串并存储到 sharedPreference 中,当您再次获取它时将其转换为简单日期
-
是的,正如 Ali 和 Amit 所说,您已将 Date 对象转换为 String 并保存。
标签: android date logic sharedpreferences