1

我有一部分代码可以找到用户以前保存的以毫秒为单位的保存时间。当时间从内存中取出然后由于某种原因转换为简单日期格式时,时间增加了 1 分钟。我不知道为什么会发生这种情况。以下是检索时间并进行转换的代码

time = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getFloat(String.valueOf(id)+"TIME", 0);
            String dateString = new SimpleDateFormat("hh:mm a").format(new Date((long) time));
            mPickedTimeText.setText(dateString);

以及节省时间的代码部分

    this.time = time.getTimeInMillis();
    PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    Editor editor=prefs.edit();
    editor.putFloat(String.valueOf(id)+"TIME", time);
    editor.commit();
    finish();

提前致谢!

4

1 回答 1

3

float数据类型没有足够的精度来表示时间,精确到一分钟。将时间存储为long

editor.putLong(String.valueOf(id)+"TIME", time);

time = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getLong(String.valueOf(id)+"TIME", 0);
String dateString = new SimpleDateFormat("hh:mm a").format(new Date(time));
于 2013-07-13T23:17:47.907 回答