Is it possible to store a Date
object using SharedPreferences
?
Actually in my code I have a String
variable, boolean
Put Date
as formatted String
, e.g.
//for putting
Date myDate;
final String FORMAT="yyyy-MM-dd";
String prefData=SimpleDateFormat(FORMAT).format(myDate);
editor.putString("Date", prefDate);
//for reading
prefDate=settings.getString("Date", "");
Date date=new SimpleDateFormat(FORMAT).parse(prefDate);
Or you can put millis as Long
editor.putLong("THE_DATE", currentDate.getTime());
And you can read a Date
from preferences like this:
long millis = sharedPreferences.getLong("THE_DATE", 0L);
Date theDate = new Date(millis);
you can store the date value using sharedpreferences like this way
editor.putLong("date",currentDate.getTime());
Set Date Time
SharedPreferences sharedPreferences = getPreferences(MODE_APPEND);
SharedPreferences.Editor editor = sharedPreferences.edit();
Date dt = getSomeDate();
editor.putLong(dateTimeKey, dt.getTime());
Get Date Time
long myDate = sharedPreferences.getLong(dateTimeKey, new Date().getTime());