android - 将日期保存到共享首选项

标签 android android-listview android-fragments sharedpreferences

我想将当前日期保存到我的 ListView 中的 TextView 我已经设置了所有其他内容所以不需要帮助只是将日期保存到 TextView 我的代码如下有人可以告诉我哪里错了吗?

SharedPreferences settings =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = settings.edit();
                editor.putLong("time", date.getTime());
                editor.commit();
                Date date2 = new Date(settings.getLong("time", 0));

最佳答案

Date 类的大部分方法已被弃用。您应该使用 Calendar 类。 日历 c = Calendar.getInstance();

要存储在您的 SharedPreferences 中,我建议您存储一个 long,它由 c.getTimeInMillis();

返回

要获取此日期并将其存储在日历对象中,您应该放置 c.setTimeInMillis(myTimeStoredInSharedPreferences)

关于android - 将日期保存到共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748341/

相关文章:

android - UIL - ImageAware 被重新用于另一个图像。任务被取消

android - 使用 fragment 的动态方式

android - 数据库经常随机删除自己

android - 从 Android 中的 strings.xml 加载时出现空指针异常

java - onBackPressed 并调用刷新函数 android

android - ListView 选择器不适用于 ListViewItem 的背景集

来自 Assets 的 Android 输入流文件 : FileNotFoundExeption

android - 有没有办法禁用除可扩展 ListView Android中单击的父 View 之外的所有父 View ?

android - 恢复 fragment

android - fragment 不会显示在 Activity 中