shi*_*ani 3 android date sharedpreferences
我需要将日期对象放在共享首选项编辑器中.
将其转换为用于存储在共享首选项中的数据类型是什么?通常我们prefEditor.putString("Idetails1", Idetails1);
为字符串和元素编写
.
我怎么做?我也可以将它用于日期对象吗?
private EditText pDisplayDate;
private ImageView pPickDate;
private int pYear;
private int pMonth;
private int pDay;
/** This integer will uniquely define the dialog to be used for 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 is 0 based so add 1
.append(pMonth + 1).append("/")
.append(pDay).append("/")
.append(pYear).append(" ")
);
}
private void displayToast() {
Toast.makeText(this,
new StringBuilder()
.append("Date choosen is ")
.append(pDisplayDate.getText()),
Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
我也可以用它作日期对象吗?
我认为你可以使用的最简单的方法是转换Date
为它的String表示.然后,您可以使用某个日期格式化程序将其简单地转换回Date对象.
String dateString = date.toString();
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(<context>);
p.edit().putString("date", dateString).commit();
Run Code Online (Sandbox Code Playgroud)
另外@MCeley如何指出,你也可以转换Date
为long并将其长值:
long dateTime = date.getTime();
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(<context>);
p.edit().putLong("date", dateTime).commit();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2178 次 |
最近记录: |