如何在SharedPreferences中存储一个日期对象?

3

我需要将日期对象放在共享首选项编辑器中。

将其转换为什么数据类型以存储在共享首选项中?通常我们写下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();
}

当然可以。您想在哪里保存和加载信息? - s_bei
@StefanBeike 我需要将它存储在SQLite中,然后上传到服务器...目前我只需要将其保存在数据库中。 - shivani
数据库与SharedPreferences没有任何关系... - s_bei
我知道,但我是这样将它存储在数据库中的:prefEditor.putString("Idetails1", Idetails1);ArrayList<String> dbvalues = new ArrayList<String>(); dbvalues.add(settings.getString("Firt_Name", "")); WayDataBase way = new WayDataBase(ProfilePage3.this); way.insertPeopleValues(dbvalues); - shivani
2个回答

6

我也可以用这个方法来处理日期对象吗?

我认为最简单的方法是将 Date 对象转换为它的字符串表示形式。然后,您可以使用一些日期格式化程序将其简单地转换回日期对象。

String dateString = date.toString();
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(<context>);
p.edit().putString("date", dateString).commit();

更新:

正如@MCeley指出的那样,您也可以将 Date 转换为长整型并将其长整型值放入:

long dateTime = date.getTime();
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(<context>);
p.edit().putLong("date", dateTime).commit();

1
长整型一直是我的首选,因为它占用的空间较少,并且不会锁定您使用一个日期字符串格式。它还不需要您使用日期格式化程序将其转换回日期。 - Michael Celey
1
@MCeley 现在我认为长整型更好,因为你不需要日期格式化程序,正如你所提到的,它占用的空间更少 - 这在移动开发中尤其重要,因为每个字节的内存都很重要。 - Simon Dorociak
如果数据库中的数据类型是日期,那么字符串会被存储在数据库中吗? - shivani
@shiv,数据库与SP有所不同。我认为SQLite不支持日期格式,只支持字符串,因此您可以将日期转换为字符串并保存到数据库中。 - Simon Dorociak

0
使用日历对象来设置日期,每当日期更改或从首选项对象中检索时,它都很容易使用。
Calendar c = Calendar.getInstance(); // use system date on first time for initialization.

long millis= pref.getLong("date",c.getTimeInMillis()); // retrieve date from Preference object in long format

c.setTimeInMillis(millis); // now set in calendar and then use it

//on date change set the year, month and day into calendar to and then stored in preference

private DatePickerDialog.OnDateSetListener pDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                pYear = year;
                pMonth = monthOfYear;
                pDay = dayOfMonth;
                c.set(pYear , pMonth , pDay ); // set into the calendar and when need to save in preference do this
                Editor edit = pref.editor();
                edit.put("date",c.getTimeInMillis());
                edit.commit();
                updateDisplay();
                displayToast();
            }
        };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接