保存当前日期

时间:2019-05-31 14:28:32

标签: java android

在保存当前日期时,我需要一些帮助。有什么方法可以保存执行该活动的当前日期和时间?

到目前为止,这是我的代码,用于声明当前日期和时间:

private String currentDateTimeString = 
DateFormat.getDateTimeInstance().format(new Date());

2 个答案:

答案 0 :(得分:1)

存储时间的最佳方法是毫秒格式。

long currentTimeMillis = System.currentTimeMillis();

然后在显示时将其转换为所需的任何格式。

轻量级数据可以存储在SharedPreference中。

编辑:

Shared Preference Tutorial

答案 1 :(得分:0)

如果您只想将活动创建的最新日期和时间保存到设备,则建议使用SharedPreferences。示例:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("DATE", currentDateTimeString);
editor.commit();

但是,如果您需要永久存储每次创建活动的日期,则应该考虑使用SQLite数据库或诸如Room之类的永久存储库

相关问题