android - How can I store a Date object in SharedPreferences? -
i need put date object in shared preferences editor.
what datatype convert storing in shared preferences? write prefeditor.putstring("idetails1", idetails1);
string , elements.
how do that? can use date object too?
private edittext pdisplaydate; private imageview ppickdate; private int pyear; private int pmonth; private int pday; /** integer uniquely define dialog used displaying date picker.*/ static final int date_dialog_id = 0; date date; private datepickerdialog.ondatesetlistener pdatesetlistener = new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { pyear = year; pmonth = monthofyear; pday = dayofmonth; updatedisplay(); displaytoast(); } }; private void updatedisplay() { pdisplaydate.settext( new stringbuilder() // month 0 based add 1 .append(pmonth + 1).append("/") .append(pday).append("/") .append(pyear).append(" ") ); } private void displaytoast() { toast.maketext(this, new stringbuilder() .append("date choosen ") .append(pdisplaydate.gettext()), toast.length_short).show(); }
can use date object too??
the simpliest way think can use convert date
string representation. can convert date object using date formatter.
string datestring = date.tostring(); sharedpreferences p = preferencemanager.getdefaultsharedpreferences(<context>); p.edit().putstring("date", datestring).commit();
update:
also how @mceley pointed out, can convert date
long , put long value:
long datetime = date.gettime(); sharedpreferences p = preferencemanager.getdefaultsharedpreferences(<context>); p.edit().putlong("date", datetime).commit();
Comments
Post a Comment