【问题标题】:How to store date in SharedPreference? [duplicate]如何在 SharedPreference 中存储日期? [复制]
【发布时间】:2020-03-16 07:17:18
【问题描述】:

我希望应用每天执行一次特定的操作,即用户在一天中第一次打开应用时。 为此,我想在 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 中存储日期?

【问题讨论】:

  • 将日期转换为字符串
  • 以毫秒为单位节省时间
  • 请打开我的问题。我的问题不一样!!!
  • @Aayushdual 如果您认为您的问题有所不同,edit 您的问题将准确解释它与标记的副本有何不同。这将允许人们投票重新开放。
  • 好的,谢谢!我等了 90 分钟再问一次!!

标签: android date logic sharedpreferences


【解决方案1】:

您可以将日期存储为时间戳:

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

然后阅读它并再次转换为Date

long timest = // read from Preferences as Long
Date date = new Date(timest);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2013-01-08
    • 2020-05-13
    • 2014-07-05
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多