如何在SharedPreferences中保存和检索日期 [英] How to save and retrieve Date in SharedPreferences
问题描述
我需要在android中的 SharedPreferences
中保存几个日期并检索它。我正在使用 AlarmManager
构建提醒应用程序,我需要保存未来日期的列表。它必须能够以毫秒为单位检索。首先,我以为计算今天和未来时间之间的时间,并以共享的偏好存储。但是,该方法不起作用,因为我需要将它用于 AlarmManager
。
要保存并加载准确的日期,可以使用 long
(/)
示例:
//获取当前时间(以毫秒为单位),并从中创建一个Date对象:
Date date = new Date(System.currentTimeMillis ()); //或者只是新的Date();
//将其转换回毫秒表示:
long millis = date.getTime();
您可以使用它来保存或检索 Date
/ 时间
数据来自 SharedPreferences
像这样
保存:
SharedPreferences prefs = ...;
prefs.edit()。putLong(time,date.getTime())。apply();
回读:
Date myDate = new Date(prefs.getLong(time,0));
修改
如果要存储 TimeZone
另外,您可以为此目的编写一些帮助方法,这样的事情(我没有测试过,可以随时更正它,如果有什么问题):
public static Date getDate(final SharedPreferences prefs,final String key,final Date defValue){
if(!prefs.contains(key +_value)||!prefs.contains(key +_zone)){
return defValue;
}
日历calendar = Calendar.getInstance();
calendar.setTimeInMillis(prefs.getLong(key +_value,0));
calendar.setTimeZone(TimeZone.getTimeZone(prefs.getString(key +_zone,TimeZone.getDefault()。getID())));
return calendar.getTime();
}
public static void putDate(final SharedPreferences prefs,final String key,final Date date,final TimeZone zone){
prefs.edit()。putLong(key + _value,date.getTime())。apply();
prefs.edit()。putString(key +_zone,zone.getID())。apply();
}
I need to save a few dates in SharedPreferences
in android and retrieve it. I am building reminder app using AlarmManager
and I need to save list of future dates. It must be able to retrieve as milliseconds. First I thought to calculate time between today now time and future time and store in shared preference. But that method is not working since I need to use it for AlarmManager
.
To save and load accurate date, you could use the long
(number) representation of a Date
object.
Example:
//getting the current time in milliseconds, and creating a Date object from it:
Date date = new Date(System.currentTimeMillis()); //or simply new Date();
//converting it back to a milliseconds representation:
long millis = date.getTime();
You can use this to save or retrieve Date
/Time
data from SharedPreferences
like this
Save:
SharedPreferences prefs = ...;
prefs.edit().putLong("time", date.getTime()).apply();
Read it back:
Date myDate = new Date(prefs.getLong("time", 0));
Edit
If you want to store the TimeZone
additionaly, you could write some helper method for that purpose, something like this (I have not tested them, feel free to correct it, if something is wrong):
public static Date getDate(final SharedPreferences prefs, final String key, final Date defValue) {
if (!prefs.contains(key + "_value") || !prefs.contains(key + "_zone")) {
return defValue;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(prefs.getLong(key + "_value", 0));
calendar.setTimeZone(TimeZone.getTimeZone(prefs.getString(key + "_zone", TimeZone.getDefault().getID())));
return calendar.getTime();
}
public static void putDate(final SharedPreferences prefs, final String key, final Date date, final TimeZone zone) {
prefs.edit().putLong(key + "_value", date.getTime()).apply();
prefs.edit().putString(key + "_zone", zone.getID()).apply();
}
这篇关于如何在SharedPreferences中保存和检索日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!