将时间存储到 SharedPreferences 的正确方法是什么? 这就是我的想法,但我收到一个错误:类型不匹配:无法从 Time 转换为 long。应该用什么格式存储?
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
long time2 = today;
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putLong("lastUpdated", time2);
editor.commit();
最佳答案
您需要维护时区本身,还是只需要表示的瞬间?如果是后者,直接调用Time.toMillis
即可。这将返回一个 long
。如果您还需要保留时区,我只需将时区 ID 存储在单独的字符串中。
然后您可以稍后通过创建一个新实例(具有适当的时区)并调用 Time.set
返回到 Time
.
(您需要根据您的精确上下文确定是为 ignoreDst
传入 true
还是 false
。有一些建议在文档中。)
关于java - 在 Android 的 SharedPreferences 中存储时间的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146864/