我想在应用程序中写入偏好更新数据的最后日期。所以我确实喜欢这个
Calendar cal = Calendar.getInstance();
SharedPreferences settings = getSharedPreferences("OrderOnline", 0);
Long updDate = settings.getLong("lastupdate", 0);
Long currdate= cal.getTimeInMillis();
if (!(updDate == currdate)) {
Log.d("AMainActivity", "updDate = " + updDate + "; currdate = " + currdate);
...
Editor ed = settings.edit();
ed.putLong("lastupdate", currdate);
ed.commit();
}
但这不是我想要的。如何在 SharedPreferences 中读取和写入当前日期?我不想使用任何已弃用的对象。谢谢你。
PS我确实想要首选项中的当前日期,而不是秒。现在 (!(updDate == currdate)) 将永远为真。
UPD 我加了这个
Calendar cal = Calendar.getInstance();
SharedPreferences settings = getSharedPreferences("OrderOnline", 0);
Long updDate = settings.getLong("lastupdate", 0)/ MILLIS_PER_DAY;
Long currdate= cal.getTimeInMillis()/ MILLIS_PER_DAY;
if (!(updDate == currdate)) {
Log.d("AMainActivity", "updDate = " + updDate + "; currdate = " + currdate);
...
Editor ed = settings.edit();
ed.putLong("lastupdate", currdate);
ed.commit();
}
进入 {} 这是结果
03-07 13:10:15.031: D/AMainActivity(9291): updDate = 15771; currdate = 15771
有些东西不行。