如何将日期存储为 SharedPreference?
How to store date as SharedPreference?
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 中存储日期?
您可以通过将日期转换为字符串来将日期保存在 SharedPreferences 中。
请通过方法"savedates"找到保存日期的例子。您将以字符串形式传递上下文和日期。上下文将是当前上下文。
public static void savedates(Context context, String date) {
SharedPreferences.Editor editor = context.getSharedPreferences("MyPref", MODE_PRIVATE).edit();
editor.putString("datestr", date);
editor.apply();
}
public static String getDates(Context context) {
SharedPreferences prefs = context.getSharedPreferences("MyPref", MODE_PRIVATE);
return prefs.getString("datestr", "");
}
//create sharedpreference object
SharedPreferences.Editor myeditor = context.getSharedPreferences("MyPref", MODE_PRIVATE).edit();
// to get current date.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
//store date in sharedpreference
myeditor.putString("mydate",date);
myeditor.commit();
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 中存储日期?
您可以通过将日期转换为字符串来将日期保存在 SharedPreferences 中。 请通过方法"savedates"找到保存日期的例子。您将以字符串形式传递上下文和日期。上下文将是当前上下文。
public static void savedates(Context context, String date) {
SharedPreferences.Editor editor = context.getSharedPreferences("MyPref", MODE_PRIVATE).edit();
editor.putString("datestr", date);
editor.apply();
}
public static String getDates(Context context) {
SharedPreferences prefs = context.getSharedPreferences("MyPref", MODE_PRIVATE);
return prefs.getString("datestr", "");
}
//create sharedpreference object
SharedPreferences.Editor myeditor = context.getSharedPreferences("MyPref", MODE_PRIVATE).edit();
// to get current date.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
//store date in sharedpreference
myeditor.putString("mydate",date);
myeditor.commit();